请求允许访问Contacts iOS的权限

Has*_*san 12 permissions contacts addressbook ios

我正在将所有本地地址簿联系人导入我的应用程序.目前,要求获得"允许访问联系人"权限的alertview在应用程序启动时完成.

如何更改它以便在应用程序的其他位置询问权限?点击按钮就像Instagram一样?

看这张图:

在此输入图像描述

AJ1*_*112 22

您可以在这里阅读文档.

以下是一些可以帮助您入门的代码:

//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
  }
Run Code Online (Sandbox Code Playgroud)

这里有另一对夫妇,你可能会喜欢看,你可以找到的教程这里这里.

希望这可以帮助!

更新:你必须使用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
    }
}
Run Code Online (Sandbox Code Playgroud)