Frage

I am using an application where i want to fetch contacts.The problem is that the app only asks for the first time that app would like to use contacts.Even if i delete the application,clean it and run again,the application is not asking the pop up next time it runs.I debugged the code and found that the permission i gave for the first time is executed.how to solve this.Below is the code i wrote in view did load.

 CFErrorRef *error=nil;
ABAddressBookRef addressbook=ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allpeople=ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex npeople=ABAddressBookGetPersonCount( addressbook );


ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        if (granted) {

            for ( int i = 0; i < npeople; i++ )
            {
                ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
                NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
                NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
                NSLog(@"Name:%@ %@", firstName, lastName);    }


            // First time access has been granted, add the contact
        } else {
            // User denied access
            // Display an alert telling user the contact could not be added
        }
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {


    for ( int i = 0; i < npeople; i++ )
    {
        ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);    }


    // The user has previously given access, add the contact
}
else {


    for ( int i = 0; i < npeople; i++ )
    {
        ABRecordRef ref = CFArrayGetValueAtIndex( allpeople, i );
        NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonFirstNameProperty));
        NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(ref, kABPersonLastNameProperty));
        NSLog(@"Name:%@ %@", firstName, lastName);    }


    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
}
War es hilfreich?

Lösung

Follow the following procedure

Go to Device's Setting->General->Reset->Reset Location & Privacy.

Then it should ask you for permission.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top