I am working on an app which required to show contacts.
It is working fine on Xcode 6.2. But when I run this project on Xcode 7.2, then it is not even asking for Permission alert.
I write following method for get permission.
-(void)requestAddressBookAccess
{
ContactsViewController * __weak weakSelf = self;
ABAddressBookRequestAccessWithCompletion(self.addressBook, ^(bool granted, CFErrorRef error)
{
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf accessGrantedForAddressBook];
});
}
});
}
It is always give granted = false
and error = nil
.
Any help will be appreciated.
In iOS 9 you have to use Contacts framework as Addressbook framework is depricated
-(void)addressbookAuthorizationUsingContacts
{
//#import #import <Contacts/Contacts.h>
// Request authorization to Address Book Using Contacts
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
//keys with fetching properties
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
NSString *phone;
NSString *fullName;
NSString *firstName;
NSString *lastName;
UIImage *profileImage;
NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
NSMutableArray *contactsArray = [[NSMutableArray alloc]init];
for (CNContact *contact in cnContacts) {
// copy data to my custom Contacts class.
firstName = contact.givenName;
lastName = contact.familyName;
if (lastName == nil) {
fullName=[NSString stringWithFormat:@"%@",firstName];
}else if (firstName == nil){
fullName=[NSString stringWithFormat:@"%@",lastName];
}
else{
fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
}
UIImage *image = [UIImage imageWithData:contact.imageData];
if (image != nil) {
profileImage = image;
}else{
profileImage = [UIImage imageNamed:@"person-icon.png"];
}
for (CNLabeledValue *label in contact.phoneNumbers) {
phone = [label.value stringValue];
if ([phone length] > 0) {
[contactNumbersArray addObject:phone];
}
}
NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
[contactsArray addObject:personDict];
NSLog(@"The contactsArray are - %@",contactsArray);
}
dispatch_async(dispatch_get_main_queue(), ^{
[tableViewContacts reloadData];
});
}
}
}];
}
The Output results are
The contactsArray are - (
{
fullName = "John Appleseed";
},
{
fullName = "Kate Bell";
},
{
fullName = "Anna Haro";
},
{
fullName = "Daniel Higgins";
},
{
fullName = "David Taylor";
},
{
fullName = "Hank Zakroff";
}
)