iphone - 以编程方式请求访问联系人

自从更新到 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/

相关文章:

ios - 从文档目录中删除指定文件

ios - Xcode MyProjectName-Bridging-Header.h 不存在

ios - Objective-C/Cocoa 中 Java 的 Thread.sleep() 等价

ios - 我如何通过 Foundation 获取星期几?

objective-c - 如何在 Objective-C 中复制对象

iphone - Objective C中变量位置的声明/定义?

ios - Xcode 9 中的 "This function declaration is not

iphone - Apple 怎么知道你在使用私有(private) API?

objective-c - 何时使用 -retainCount?

objective-c - 最佳实践 - 您自己的项目/应用程序的 NSError 域和代码