As per apple's website (scroll down to Privacy in the middle of the page), access to the address book must be granted before it can be access programmatically.Here is what I ended up doing-
#import <AddressBookUI/AddressBookUI.h>
- (BOOL)isContactsFetchingAccessibleOrNot {
// Request authorization to Address Book
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
// First time access has been granted, add the contact
//do something
} else {
// User denied access
// Display an alert telling user the contact could not be added
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
//do something
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
}
You can also call this method when app comes from background to foreground.