My app uses Address Book framework. To show all contacts in iOS 6 or higher I have to set permission for it.
At first time when I was running my app on the simulator I saw this alert below, but when I try to show it again I don't see the alert one more time.
I have reseted content and settings but it has not helped me. So when I rerun app it all time shows contacts, but at first the app has to show alert when I reseted simulator. Anybody faced with this issue?
Code below:
- (void)getPersonOutOfAddressBook
{
if (self.tableData) {
[self.tableData removeAllObjects];
}
ABAddressBookRef addressBook = ABAddressBookCreate();
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
dispatch_release(sema);
}
else
{
// we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted)
{
if (addressBook != nil)
{
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray
*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i < [allContacts count]; i++)
{
Contact *contact = [[Contact alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString
*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString
*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@",
firstName, lastName];
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(contactPerson);
contact.firstName = firstName;
contact.lastName = lastName;
contact.fullName = fullName;
contact.image = [UIImage imageWithData:imgData];
[self.tableData addObject:contact];
}
CFRelease(addressBook);
}
}
}