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

时间:2015-07-17 10:08:48

标签: ios objective-c iphone ipad

我正在iOS中访问用户联系人。我调用了一个访问用户联系人的函数。当调用此函数时,会显示一个警告对话框。要求用户允许访问或不访问联系人。如果用户允许访问,我的代码工作正常。如果用户不允许访问,那么我想再次显示询问用户访问联系人权限的对话框。我正在使用以下代码访问用户联系人。请告诉我该怎么做。

代码:

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];

 }

3 个答案:

答案 0 :(得分:3)

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

你得到的可能性是:

    iOS8及更高版本上的
  • :使用UIApplicationOpenSettingsURLString提供的深层链接在应用程序设置页面上重定向用户

    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    
  • 以前的iOS版本
  • :创建一个弹出窗口,警告用户该应用程序无法访问联系人列表,并描述授予访问权限的方式(转到设置 - >您的应用 - >联系...)

答案 1 :(得分:1)

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

答案 2 :(得分:0)

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