iphone - 更改 ABRecordRef 属性

标签 iphone ios objective-c ipad

我有以下cpde:

ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressBook, ABRecordGetRecordID(self.recordRef_));

 CFErrorRef  error = NULL;

    if ([self.nameTextField_.text isNotNull]){
        NSArray *nameStringArray = [self.nameTextField_.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)([nameStringArray objectAtIndex:0]), NULL);
        if ([nameStringArray count] > 1){
            ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)([nameStringArray lastObject]), &error);
        }
    }

但是,在这段代码之后它给了我这个错误:

Error Domain=ABAddressBookErrorDomain Code=0 "The operation couldn’t be completed. (ABAddressBookErrorDomain error 0.)"

知道这是为什么吗?

最佳答案

错误消息似乎毫无用处,但由于您的其余代码看起来是有效的,我猜测您的应用程序未被用户授权访问地址簿数据库。在尝试访问地址簿之前,使用如下代码检查您的授权状态:

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // We're good to go
} else {
    // Hasn't been authorized by the user
    // You can check the exact authorization status for more information on the
    // exact reason why you can't access the Address Book (e.g. denied, restricted, etc.)
}

关于iphone - 更改 ABRecordRef 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14804279/

相关文章:

php - iPhone 应用程序中的远程服务器、php、MySQL 连接

objective-c - 使用 NSDictionary,您可以确定值的数据类型吗?

ios - ABAddressBookSave 导致 EXC_BAD_ACCESS ARC 已启用

ios - 如何根据设备更改 View 高度? (xcode)

iphone - 我正在尝试初始化要推送的 UIViewController

ios - iPhone,iOS 8 : How to presentViewController smaller than original view controller?

ios - 有没有办法在不运行应用程序的情况下查看 Storyboard场景?

c++ - 关于在何处在线学习 C 以准备学习 Objective-C 的任何建议

iphone - 使用 getter 访问 Objective-C 中结构实例变量的元素

ios - 向 UIWindow 添加一个按钮。最佳实践?