0

我想将联系人添加到我的通讯录中。我正在开发一个应用程序,我已经通过了许多链接,并且我有以下代码,但现在我被卡住了。

我已经导入:

    #import <AddressBook/AddressBook.h>
    #import <AddressBookUI/AddressBookUI.h

viewcontroller.m

-(IBAction)addToAddressbook:(id)sender{
ABUnknownPersonViewController *unknownPersonViewController = [[ABUnknownPersonViewController alloc] init];
unknownPersonViewController.displayedPerson = (ABRecordRef)[self buildContactDetails];
unknownPersonViewController.allowsAddingToAddressBook = YES;
[self.navigationController pushViewController:unknownPersonViewController animated:YES];
[unknownPersonViewController release];
}

- (ABRecordRef)buildContactDetails {
NSLog(@"building contact details");
ABRecordRef person = ABPersonCreate();
CFErrorRef  error = NULL;

// firstname
ABRecordSetValue(person, kABPersonFirstNameProperty, @"Don Juan", NULL);

// email
ABMutableMultiValueRef email = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(email, @"expert.in@computer.com", CFSTR("email"), NULL);
ABRecordSetValue(person, kABPersonEmailProperty, email, &error);
CFRelease(email);

// Start of Address
ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
NSMutableDictionary *addressDict = [[NSMutableDictionary alloc] init];
[addressDict setObject:@"The awesome road numba 1" forKey:(NSString *)kABPersonAddressStreetKey];
[addressDict setObject:@"0568" forKey:(NSString *)kABPersonAddressZIPKey];
[addressDict setObject:@"Oslo" forKey:(NSString *)kABPersonAddressCityKey];
ABMultiValueAddValueAndLabel(address, addressDict, kABWorkLabel, NULL);
ABRecordSetValue(person, kABPersonAddressProperty, address, &error);
[addressDict release];
CFRelease(address);
// End of Address

if (error != NULL)
    NSLog(@"Error: %@", error);

[(id)person autorelease];
return person;
}

UI我有一个IBAction连接到的按钮addToAddressbook,但点击时,什么都没有发生 - 那么我还需要UI在代码中或代码中做什么?

4

1 回答 1

0

查看您的代码后,我可以看到您缺少该ABAddressBookSave语句。设置值后

// firstname
ABRecordSetValue(person, kABPersonFirstNameProperty, @"Don Juan", NULL);

// email
ABRecordSetValue(person, kABPersonEmailProperty, email, &error);

// Address
ABRecordSetValue(person, kABPersonAddressProperty, address, &error);

你应该调用ABAddressBookSave如下。

ABAddressBookSave(ABAddressBookRef addressBook, CFErrorRef* error);
于 2013-05-24T06:36:41.110 回答