Question

I would like my app to only ask the user for permission to access the Contacts addressbook until the user access the proper function in my app. I really don't want to request permission when the app loads.

As a result I've used the following code:

- (IBAction)importClientsButtonPressed:(id)sender {
// request access to Contacts address book
CFErrorRef addyError = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &addyError);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef addyError) {
    });
}

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    _importContactsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Import Client from Contacts"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Primary Contact", @"Secondary Contact", nil];
    _importContactsActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

    [_importContactsActionSheet showFromRect:self.importClientsButton.frame inView:self.importClientsButton.superview animated:YES];
} else {
    // the user has previously denied access - send alert to user to allow access in Settings app
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Settings"
                                                    message:@"This app does not have access to your contacts.  You can enable access in Privacy Settings."
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

}

However, the permission dialog does not halt the app and wait for a response...the code following the request continues to run. As a result I get a whack of messages popping up out of order.

Is there any way to have the whole app halt until a response comes back from the permission request dialog?

Thanks!

Était-ce utile?

La solution

In your code above also you have this:

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef addyError)  {
    if(granted) {
        //Put here your code
    }
});

so finally i would write the code in this way:

- (IBAction)importClientsButtonPressed:(id)sender {
    __weak typeof(self) weakSelf = self;

    // request access to Contacts address book
    CFErrorRef addyError = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &addyError);
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef addyError)  {
            if(granted) {
                [weakSelf openImportContact];
            }
        });
    } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        [weakSelf openImportContact];
    } else {
        // the user has previously denied access - send alert to user to allow access in Settings app
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Settings"
                                                        message:@"This app does not have access to your contacts.  You can enable access in Privacy Settings."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}


- (void)openImportContact {
    dispatch_async(dispatch_get_main_queue(), ^{
        _importContactsActionSheet = [[UIActionSheet alloc] initWithTitle:@"Import Client from Contacts"
                                                         delegate:self
                                                cancelButtonTitle:@"Cancel"
                                           destructiveButtonTitle:nil
                                                otherButtonTitles:@"Primary Contact", @"Secondary Contact", nil];
        _importContactsActionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;

        [_importContactsActionSheet showFromRect:self.importClientsButton.frame inView:self.importClientsButton.superview animated:YES];
    });
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top