在iOS 6中无法访问设备上的联系人源

Cli*_*ris 14 addressbook ios

这个代码在iOS 5.1上运行正常,也可以在iOS 6的iPhone模拟器中运行.它在运行iOS 6的iPhone 4上无声地失败.最终结果是我无法将人添加到Contacts应用程序中.以下代码片段都不起作用(每个日志跟随):

ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(_addressBook);
NSLog(@"2 - defaultSource = %@", defaultSource);
Run Code Online (Sandbox Code Playgroud)

AB:无法编译查询语句(ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):SELECT ROWID,Name,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 error =操作无法完成.(ABAddressBookErrorDomain错误1.)


我以为我可以获得所有的来源,然后选择一个,但以下都没有返回任何:

CFArrayRef allSources = ABAddressBookCopyArrayOfAllSources (_addressBook);
NSLog(@"2 - allSources = %@", allSources);
Run Code Online (Sandbox Code Playgroud)

AB:无法编译查询语句(ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):SELECT ROWID,Name,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 =()

Rya*_*anG 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
  }
Run Code Online (Sandbox Code Playgroud)


Taf*_*soh 3

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

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

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