Search code examples
objective-cabaddressbookios10

ABAddressBookRequestAccessWithCompletion crashes on iOS 10


I use below code to access contacts in my iOS application. It was working fine in iOS<10 but with Xcode 8 and iOS 10 it crashes:

- (void)btcContacts_tap {
    ABAddressBookRef addressBook =  ABAddressBookCreateWithOptions(NULL, NULL);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        if (granted) {
            _addressBookController = [[ABPeoplePickerNavigationController alloc] init];

            [[_addressBookController navigationBar] setBarStyle:UIBarStyleBlack];

            _addressBookController.delegate = self;
            [_addressBookController setPredicateForEnablingPerson:[NSPredicate predicateWithFormat:@"%K.@count > 0", ABPersonPhoneNumbersProperty]];
            [_addressBookController setPeoplePickerDelegate:self];
            [self presentViewController:_addressBookController animated:YES completion:nil];
        }
        else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self showMessage:NSLocalizedStringFromTable(@"PLEASE_GRANT_CONTACTS", LIApplicationLanguage(), nil) andAdvertise:@"" andService:nil andTransactionState:kTTTransactionStateInfo];
            });
        }
    });
}

I have set NSSetUncaughtExceptionHandler to a method for logging the crash report but even the exception handler is not calling...

Does someone else faced this problem too?


Solution

  • iOS 10:

    You need to put the NSContactsUsageDescription in your plist. Like:

    <key>NSContactsUsageDescription</key>
    <string>$(PRODUCT_NAME) uses photos</string>
    

    See all usage descriptions here.