自从更新到 iOS 6 后,我注意到我将联系人添加到 iPhone 通讯录的代码不再有效。我认为这是与权限相关的问题,因为 Apple 现在需要用户权限才能访问联系人(修复 this 问题)。
我希望该应用会自动请求访问联系人的权限,如下面的屏幕截图所示,但事实并非如此。尝试添加联系人失败,ABAddressBookErrorDomain 错误 1
。
我是否需要以编程方式启动访问联系人请求对话框?这是怎么做到的?
最佳答案
根据 this documentation在苹果的网站上(向下滚动到页面中间的隐私),对通讯簿的访问必须被授予才能以编程方式访问。这就是我最终要做的。
#import <AddressBookUI/AddressBookUI.h>
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
[self _addContactToAddressBook];
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self _addContactToAddressBook];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
适用于 iOS 9 及更高版本的更新:
来自苹果网站:
Important
The Address Book UI framework is deprecated in iOS 9. Use the APIs defined in the ContactsUI framework instead. To learn more, see ContactsUI
https://stackoverflow.com/questions/12648244/