Brian Scherady
Brian Scherady

Reputation: 61

IOS Objective-c - Getting contacts not working - No request prompt - Access denied

I am using Objective-c to develop an app for iPad. I need to fetch the address book for the contacts. But I get no access request prompt and the access stays denied. The boolean "granted" is never true and the code to get the contacts array is never reached. Therefore the contacts array contactsArray stays empty.

Following is the code I am using:

-(void) fetchAllContacts
{
    contactsArray = [[NSMutableArray alloc] init];

    CNContactStore *store = [[CNContactStore alloc] init];

    [store requestAccessForEntityType : CNEntityTypeContacts completionHandler : ^(BOOL granted, NSError * _Nullable error)
    {
        if (granted)
        {
             // Code to get the contacts array
             // contactsArray = ....
        }       
    }];
}

Any help?

Thank you

Upvotes: 1

Views: 458

Answers (1)

pepsy
pepsy

Reputation: 1457

iOS will only present the modal access request prompt once. If you have denied the access the first time, the app will be unable to access it until the user changes the app's permissions in the iOS settings.

One option is to present a custom prompt saying access is denied with a button to navigate directly to the app settings page, using UIApplicationOpenSettingsURLString as an URL.

//objc
NSURL * url = [[NSURL alloc] initWithString:UIApplicationOpenSettingsURLString];
[UIApplication.sharedApplication openURL:url];

//swift
if let url = URL(string: UIApplicationOpenSettingsURLString) {
    UIApplication.shared.openURL(url)
}

Upvotes: 1

Related Questions