【问题标题】:Ask for permission to allow access to Contacts iOS [duplicate]请求允许访问联系人 iOS [重复]
【发布时间】:2013-10-20 16:46:36
【问题描述】:

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

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

看这张图:

【问题讨论】:

标签: ios permissions contacts addressbook


【解决方案1】:

您可以阅读文档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
    }
}

【讨论】:

  • 酷,我如何停止应用程序在启动时请求访问权限?
  • @Hassan 查看我关于如何检测您的应用首次启动的更新。在 else 部分,您可以编写一些代码来不要求联系人访问权限。
  • 真的帮了我!谢谢大家
【解决方案2】:

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

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

【讨论】:

  • 在请求许可时有没有一种方法?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-18
  • 1970-01-01
  • 2013-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2017-06-27
相关资源
最近更新 更多