TechChain
TechChain

Reputation: 8952

Issue in accessing user contacts in iOS

I am accessing user contacts in iOS.I have called a function which access the user contacts.An alert dialog is shown when this function is called & asking to user allow access to contacts or not.My code works fine if user allow the access.If user don't allow access then i want to again show that dialog of asking permission to user for accessing contacts.I am using following code for accessing the user contacts.Please tel me what to do.

code:

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

 }

Upvotes: 0

Views: 303

Answers (3)

Jems
Jems

Reputation: 197

Unfortunately it's not possible to show the permission popup twice in an iOS application. It's the same strategy for Location, Calendar, Photos, etc.

The possibility you've got is:

  • on iOS8 and later: Redirect the user on the application setting page, using the deep link provided by UIApplicationOpenSettingsURLString

    UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
    
  • on previous iOS version: Create a popup to warn the user that the application has no access to the contact list, and describe the way to grant access (Go to Setting-> Your App -> Contact ...)

Upvotes: 3

user3182143
user3182143

Reputation: 9609

Kindly call this below code in your viewDidLoad method or wherever you want apply this below coding

  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
  }

Upvotes: 0

Eugene
Eugene

Reputation: 10045

The "allow contacts access" alert that is being shown to user in your app is managed by the system. It's shown once when you initially request access to contacts, user can either permit or deny the access. This alert will never be shown again once user has made a choice. If contacts access has been denied once, the only way to permit access to the contacts then is to go to Settings app and enable access using a switch manually via Contacts section in Privacy tab.

Upvotes: 1

Related Questions