iOS 8 添加联系人到地址簿

标签 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,因为没有注册错误。

问题是 [pp addressBook] 正在返回 nil。所以你的 ABAddressBookRef addressBook 引用是 nil

解决方法是使用 ABAddressBookCreateWithOptions 而不是 ABPeoplePickerNavigationController[pp addressBook] 方法。

这是一个在 iOS 7.1 和 iOS 8.1 上都运行良好的示例:

-(void)requestAuthorizationAndAddPersonToAddressBook 
{
// Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            [self addPersonToAddressBook];
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        [self addPersonToAddressBook];
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
}

-(void)addPersonToAddressBook {


    NSString * fullName = @"James Bond";

    CFErrorRef abCreateError = nil;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &abCreateError);

    if (abCreateError) {
        NSLog(@"Error occurred: %@", abCreateError);
    }

    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];
    }

         if (cfError) {
              NSLog(@"error is %@", cfError);
        }
    }

关于iOS 8 添加联系人到地址簿,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196882/

相关文章:

javascript - react native 运行ios类型错误: invalid data

ios - 将三个 iOS 应用程序合并为一个

ios - 如何在 objective-c 中使用 nsurl 在 url 查询参数中传递数组?

iOS 8 - 带有渐变色文本的 UILabel

ios - 强制 UITextView 正确换行

iOS - 是否可以缓存分块的 HTTP 响应?

ios - 如何在 iOS 中删除 UITableViewCell 的 No Result TableView?

iOS 7 TableView 就像 iPad 上的“设置”应用程序中一样

ios - iOS 中的 SOAP 网络服务

uitableview - 创建动画,例如 iOS 8 Weather App