12

我正在将所有本机通讯录联系人导入我的应用程序。目前,请求“允许访问联系人”权限的警报视图是在应用程序启动时完成的。

如何更改它以便在应用程序的其他地方询问权限?只需单击一个按钮,就像 Instagram 一样?

看这张图片:

在此处输入图像描述

4

2 回答 2

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
  }

以下是您可能希望看到的另外几个教程,您可以在此处此处找到。

希望这可以帮助!

更新:您必须使用 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
    }
}
于 2013-10-12T19:51:29.100 回答
0

应用程序在您尝试检索联系人时请求许可。

只需在按钮触摸后调用 retriving,而不是在启动后调用。

于 2013-10-13T08:41:32.043 回答