14

此代码在 iOS 5.1 上运行良好,并且在 iOS 6 的 iPhone 模拟器中也可以运行。它在运行 iOS 6 的 iPhone 4 上静默失败。最终结果是我无法将人员添加到联系人应用程序。以下代码片段都不起作用(每个都跟在日志后面):

ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(_addressBook);
NSLog(@"2 - defaultSource = %@", defaultSource);

AB:无法编译查询语句(ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):SELECT ROWID、名称、ExternalIdentifier、Type、ConstraintsPath、ExternalModificationTag、ExternalSyncTag、AccountID、Enabled、SyncData、MeIdentifier、Capabilities FROM ABStore WHERE Enabled = ?;

2012-09-24 11:00:36.731 QR vCard[193:907] 2 - defaultSource = (CPRecord: 0x1f59fd50 ABStore)

当我尝试将一个人添加到通讯簿时,我得到了这个(似乎是因为来源无效,即使从上面看起来它可能是好的):

2012-09-24 11:18:32.231 QR vCard[220:907] ABAddressBookAddRecord 错误 = 操作无法完成。(ABAAddressBookErrorDomain 错误 1。)


我以为我可以获取所有来源,然后选择一个,但以下内容根本没有返回:

CFArrayRef allSources = ABAddressBookCopyArrayOfAllSources (_addressBook);
NSLog(@"2 - allSources = %@", allSources);

AB:无法编译查询语句(ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):SELECT ROWID、名称、ExternalIdentifier、Type、ConstraintsPath、ExternalModificationTag、ExternalSyncTag、AccountID、Enabled、SyncData、MeIdentifier、Capabilities FROM ABStore WHERE Enabled = ?;

2012-09-24 10:58:09.908 QR vCard[177:907] 2 - allSources = ()

4

3 回答 3

24

我遇到了同样的问题,我无法弹出允许访问联系人警报。

Kyle 在这里发布了答案: https ://stackoverflow.com/a/12648938/480415

  // 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
    });
  }
  else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
  }
  else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
  }
于 2012-10-03T01:52:11.680 回答
3

此日志消息表明您的应用程序未被(可能尚未)允许访问联系人。iOS 6 允许用户拒绝应用程序访问地址簿的权限。

一旦用户允许您的应用程序访问联系人,该消息就会消失 - 通过弹出对话框或转到设置 -> 隐私 -> 联系人。

有关此主题的更多信息,请参阅 WWDC 2012 会议 710“iOS 和 OS X 中的隐私支持”。

于 2012-09-26T15:58:25.787 回答
0

如果您从 Google 来到这里,并且正在使用 iOS 的新CNContactStore框架,并且遇到这些错误,请继续阅读:

我认为让我的 CNContactStore 成为使用类实例初始化的成员变量会更干净:

class foo {
    var contactStore = CNContactStore()

    func findByIdentifier(identifier: String) -> CNContact {
        let contact = try self.contactStore.unifiedContactWithIdentifier(identifier...
        return contact
    }
}

在我打了大约五十次之后,它开始出错

AB:无法编译查询语句 (ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties)

我尝试对我的通话进行限速,但这并没有帮助。事实证明,为每次调用实例化一个新的 CNContactStore 对性能的影响为零,并且完全解决了我的问题:

class foo {

    func findByIdentifier(identifier: String) -> CNContact {
        let contactStore = CNContactStore()
        let contact = try contactStore.unifiedContactWithIdentifier(identifier...
        return contact
    }
}

希望这可以帮助!

于 2016-04-08T17:54:17.903 回答