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];
});
}