请求允许访问联系人iOS

前端之家收集整理的这篇文章主要介绍了请求允许访问联系人iOS前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > Programmatically Request Access to Contacts10个
我将所有本地通讯录联系人导入我的应用程序.目前,要求允许访问联系人的警报视图是在应用启动时完成的.

如何更改它,因此在应用程序的其他地方要求许可?点击一个按钮,就像Instagram?

看到这张照片:

解决方法

您可以阅读文档 HERE.

这里有一些代码让你开始:

//First of all import the AddRessBookUI 
  #import <AddressBookUI/AddressBookUI.h>

  // Request to authorise the app to use addressbook
  ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(nil,nil);

  if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef,^(bool granted,CFErrorRef error) {
      if (granted) {
          // If the app is authorized to access the first time then add the contact
          [self _addContactToAddressBook];
      } else {
          // Show an alert here if user denies access telling that the contact cannot be added because you didn't allow it to access the contacts
      }
    });
  }
  else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // If the user user has earlier provided the access,then add the contact
    [self _addContactToAddressBook];
  }
  else {
    // If the user user has NOT earlier provided the access,create an alert to tell the user to go to Settings app and allow access
  }

这里有另外几个教程,您可能希望看到可以找到HEREHERE.

希望这可以帮助!

更新:您必须使用didFinishLaunchingWithOptions检测您的应用程序第一次启动:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasLaunchedOnce"])
    {
        // The app has already launched once
    }
    else
    {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasLaunchedOnce"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        // This is the first time the app is launched
    }
}

猜你在找的iOS相关文章