iOS 8添加联系人到地址簿

时间:2014-10-04 20:29:09

标签: ios objective-c ios8 addressbook abaddressbook

我试图在iOS8中向地址簿添加联系人。无法再这样做了。这是我的代码:

 -(void)addPersonToAddressBook {


NSString * fullName = integrationDictionary[@"fullName"];

ABPeoplePickerNavigationController *pp =[ABPeoplePickerNavigationController new];
ABAddressBookRef addressBook = [pp addressBook];
ABRecordRef entry = ABPersonCreate();
CFErrorRef cfError=nil;

ABRecordSetValue(entry, kABPersonFirstNameProperty, (__bridge CFTypeRef)(fullName) , nil);

ABAddressBookAddRecord(addressBook, entry, &cfError);

if (ABAddressBookSave(addressBook, &cfError)) {
    NSString *saveMessage = [NSString stringWithFormat:@"%@ has been added to your address book.", fullName];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact Added" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
} else {
    NSString *saveMessage = [NSString stringWithFormat:@"There was an error adding %@ to your address book.", fullName];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Uh Oh" message:saveMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
}

NSLog(@"error is %@", cfError);

错误显示为null。谁看过这个吗?任何解决方法?

1 个答案:

答案 0 :(得分:2)

错误正在返回NULL,因为没有注册错误。

问题是[pp addressBook]正在返回nil。因此,ABAddressBookRef addressBook引用为nil

解决方法是使用ABAddressBookCreateWithOptions代替[pp addressBook]的{​​{1}}方法。

这是一个在iOS 7.1和iOS上运行良好的样本。 iOS 8.1:

ABPeoplePickerNavigationController