I have several calls to ABAddressBookCreate() in the app I'm working on. For iOS >=6 compatibility I thought to use a singleton to check for access to the address book and to return a ABAddressBookRef from the singleton. However I'm not sure how correctly release the ABAddressBookRef instance or whether the this approach is safe to use.
+(ABAddressBookRef)loadContacts {
ABAddressBookRef ref;
if ([self isABAddressBookCreateWithOptionsAvailable]) {
CFErrorRef error = nil;
ref = ABAddressBookCreateWithOptions(NULL,&error);
ABAddressBookRequestAccessWithCompletion(ref, ^(bool granted, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
[self showErrorDialog:@"Error"];
} else if (!granted) {
[self showErrorDialog:@"Not granted"];
}
});
});
// [(id)ref autorelease]; not working..
return ref;
} else {
// iOS 4/5
ref = ABAddressBookCreate();
// [(id)ref autorelease];
return ref;
}
}
Call in ViewController:
ABAddressBookRef addressBook = [AddressBookAccess loadContacts];