访问 iOS 中的用户联系人时出现问题

Issue in accessing user contacts in iOS

我正在访问 iOS.I 中的用户联系人调用了一个访问用户的函数 contacts.An 调用此函数时显示警告对话框并要求用户允许访问联系人或 not.My 如果用户允许 access.If 用户不允许访问,代码工作正常然后我想再次显示请求用户访问权限的对话框 contacts.I 我正在使用以下代码访问用户 contacts.Please 告诉我该怎么做。

代码:

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
        {
            NSLog(@"acess:first block");
            // First time access has been granted, add the contact
            access=[NSUserDefaults standardUserDefaults];
            [access setObject:@"false" forKey:@"contact_access"];
            [access synchronize];
        });
 }
 else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
 {
            NSLog(@"acess:second block");
        // The user has previously given access, add the contact
        access=[NSUserDefaults standardUserDefaults];
        [access setObject:@"true" forKey:@"contact_access"];
        [access synchronize];
 }

 else
 {
        NSLog(@"acess:third block");
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings appa
        access=[NSUserDefaults standardUserDefaults];
        [access setObject:@"false" forKey:@"contact_access"];
        [access synchronize];
        UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please allow access to contacts to use rolodex feature" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];

 }

在您的应用中向用户显示的 "allow contacts access" 警报由系统管理。当您最初请求访问联系人时显示一次,用户可以允许或拒绝访问。一旦用户做出选择,此警报将永远不会再次显示。如果联系人访问被拒绝一次,则允许访问联系人的唯一方法是转到“设置”应用并通过“隐私”选项卡中的“联系人”部分手动使用开关启用访问。

请在你的 viewDidLoad 方法中调用下面的代码,或者在任何你想应用下面代码的地方

  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

      } 
      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

  }
  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
  }

遗憾的是,无法在 iOS 应用程序中显示两次权限弹出窗口。位置、日历、照片等的策略相同

你得到的可能性是:

  • on iOS8 及更高版本:使用 UIApplicationOpenSettingsURLString

    提供的深层 link 在应用程序设置页面上重定向用户
    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    
  • 上一个 iOS 版本:创建一个弹出窗口警告用户该应用程序无权访问联系人列表,并描述授予访问权限的方式(转到“设置”->“您的应用程序”->“联系...”)