1

I am using The Following code for getting iPhone Contacts but my App is not getting Permission For Allow Contacts in iOS 9 . I have found this code from stack and the other references are also same .

   - (void)getPersonOutOfAddressBook
{
    //1
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    if (addressBook != nil) {
        NSLog(@"Succesful.");

        //2
        NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

        //3
        NSUInteger i = 0; for (i = 0; i < [allContacts count]; i++)
        {
            NSMutableDictionary *persiondict  =[[NSMutableDictionary alloc]init] ;
//            Person *person = [[Person alloc] init];
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

            //4
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson,
                                                                                  kABPersonFirstNameProperty);
            NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
            NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];

//            person.firstName = firstName;
//            person.lastName = lastName;
//            person.fullName = fullName;
            [persiondict setValue:fullName  forKey:@"fullName"] ;

            //email
            //5
            ABMultiValueRef emails = ABRecordCopyValue(contactPerson, kABPersonEmailProperty);

            //6
            NSUInteger j = 0;
            for (j = 0; j < ABMultiValueGetCount(emails); j++) {
                NSString *email = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(emails, j);
                if (j == 0) {
//                    person.homeEmail = email;
                    [persiondict setValue:email  forKey:@"email"] ;

//                    NSLog(@"person.homeEmail = %@ ", person.homeEmail);
                }
                else if (j==1)
                [persiondict setValue:email  forKey:@"email"] ;

            }

            //7 
            [ArrUserOfContacts addObject:persiondict];
        }

        //8
        CFRelease(addressBook);
    } else { 
        //9
        NSLog(@"Error reading Address Book");
    } 
}

I am unable to find problem here , How can user get an permission for access contacts . Any Suggestion Will be helpfull .

guru
  • 2,727
  • 3
  • 27
  • 39

3 Answers3

4

ABAddressBookRequestAccessWithCompletion is deprecated in iOS 9. Now you should use Contacts framework. This is an example in Objective C:

CNContactStore * contactStore = [CNContactStore new];
[contactStore requestAccessForEntityType:entityType completionHandler:^(BOOL granted, NSError * _Nullable error) {
             if(granted){
                 //
             }
         }];

In Swift 3:

CNContactStore().requestAccess(for: .contacts, completionHandler: { granted, error in
        if (granted){
             //
        }
    })

This will only ask for permission if the user hasn't denied or approved permissions for contacts in your app. You can't ask for permissions that have already been denied by the user (At least now in iOS 10), what you can do is redirect the user to Settings.

YYamil
  • 1,100
  • 11
  • 11
3

You need request permissions using ABAddressBookRequestAccessWithCompletion()

ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
  if (!granted){
    NSLog(@"Just denied");
    return; 
  }
  NSLog(@"Just authorized");
});
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39
0

If you want to check user given contacts permission or not and, if permission is not given then show alert to move user in settings to give permission.

Then use the following function checkContactsPermission as:

-(void)checkContactsPermission {

    //Check permission status
    switch (ABAddressBookGetAuthorizationStatus()) {
        case kABAuthorizationStatusAuthorized:

            // Already permission given

            break;
        case kABAuthorizationStatusDenied:{

            // Permission not given so move user in settings page to app.

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
                                                                     style:UIAlertActionStyleDefault
                                                                   handler:^(UIAlertAction * action)
                                             {
                                                 NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];

                                                 if (settingsURL) {
                                                     [[UIApplication sharedApplication] openURL:settingsURL];
                                                 }
                                             }];

            UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
                                                                   style:UIAlertActionStyleDefault
                                                                 handler:^(UIAlertAction * action)
                                           {

                                           }];

            [alert addAction:SettingsButton];
            [alert addAction:DeniedButton];

            [self presentViewController:alert animated:YES completion:nil];
        }

        case kABAuthorizationStatusRestricted: {

            // Permission not given so move user in settings page to app.

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert!" message:@"his app requires access to your contacts." preferredStyle:UIAlertControllerStyleAlert];

            UIAlertAction* SettingsButton = [UIAlertAction actionWithTitle:@"Settings"
                                                                style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action)
                                        {
                                            NSURL * settingsURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",UIApplicationOpenSettingsURLString,[[NSBundle mainBundle]bundleIdentifier]]];

                                            if (settingsURL) {
                                                [[UIApplication sharedApplication] openURL:settingsURL];
                                            }
                                        }];

            UIAlertAction* DeniedButton = [UIAlertAction actionWithTitle:@"Denied"
                                                               style:UIAlertActionStyleDefault
                                                             handler:^(UIAlertAction * action)
                                       {

                                       }];

            [alert addAction:SettingsButton];
            [alert addAction:DeniedButton];

            [self presentViewController:alert animated:YES completion:nil];
        }
            break;

        case kABAuthorizationStatusNotDetermined:

             // Permission not determin. so request for permission.
            ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
                if (granted){

                    // Already permission given
                }
            });

            break;
    }
}

for iOS 10 you can use Contacts framework for check permission.

Nikhlesh Bagdiya
  • 4,316
  • 1
  • 19
  • 29