在iOS 6中无法访问设备上的联系人源
此代码在iOS 5.1上运行正常,并且在iOS 6的iPhone模拟器中也能正常工作。它在运行iOS 6的iPhone 4上无声地失败。最终结果是我无法将人添加到联系人应用程序中。以下代码片段都不起作用(每个日志跟踪):
This code worked OK on iOS 5.1 and also does work in the iPhone simulator with iOS 6. It fails silently on my iPhone 4 running iOS 6. The end result is that I cannot add a person to the Contacts app. Neither of the following code snippets work (log follows each):
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(_addressBook);
NSLog(@"2 - defaultSource = %@", defaultSource);
AB:无法编译查询语句(ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):
SELECT ROWID,Name, ExternalIdentifier,Type,ConstraintsPath,ExternalModificationTag,ExternalSyncTag,AccountID,Enabled,SyncData,MeIdentifier,Capients FROM ABStore WHERE Enabled =?;
AB: Could not compile statement for query (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:00:36.731 QR vCard[193:907] 2 - defaultSource = (CPRecord: 0x1f59fd50 ABStore)
当我尝试将一个人添加到地址簿时我得到了这个(似乎是因为来源无效,即使从上面看起来可能没问题):
When I try to add a person to the Address Book I get this (seems to be because the source is invalid, even though it looks like it might be OK from the above):
2012-09-24 11:18:32.231 QR vCard [220 :907] ABAddressBookAddRecord error =无法完成操作。 (ABAddressBookErrorDomain错误1。)
2012-09-24 11:18:32.231 QR vCard[220:907] ABAddressBookAddRecord error = The operation couldn’t be completed. (ABAddressBookErrorDomain error 1.)
我以为我可以获得所有来源然后选择一个,但以下返回无完全:
I thought I could get all the sources and then pick one, but the following returns none at all:
CFArrayRef allSources = ABAddressBookCopyArrayOfAllSources (_addressBook);
NSLog(@"2 - allSources = %@", allSources);
AB:无法编译查询语句(ABCCopyArrayOfAllInstancesOfClassInSourceMatchingProperties):
SELECT ROWID,Name, ExternalIdentifier,Type,ConstraintsPath,ExternalModificationTag,ExternalSyncTag,AccountID,Enabled,SyncData,MeIdentifier,Capients FROM ABStore WHERE Enabled =?;
AB: Could not compile statement for query (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 =()
2012-09-24 10:58:09.908 QR vCard[177:907] 2 - allSources = ()
我有同样的问题,我无法得到允许访问联系人提醒弹出窗口。
I had the same issue and I could not get the Allow Access To Contacts alert to popup.
答案由Kyle发布:
https://stackoverflow.com/a/12648938/480415
The Answer was posted by Kyle here: 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
}