Reputation: 1154
I am developing an application in which I want to get all contacts from my iphone including detail (First Name, Last Name, Email and Mobile Number). The deployment target of application is iOS 7 and later.
I had tried a below code but its not working at all.
My code is here
__block NSMutableArray *myContacts = [[NSMutableArray alloc]init];
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL userDidGrantAddressBookAccess;
CFErrorRef addressBookError = NULL;
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized )
{
addressBook = ABAddressBookCreateWithOptions(NULL, &addressBookError);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
{
userDidGrantAddressBookAccess = granted;
dispatch_semaphore_signal(sema);
if (addressBook!=nil)
{
NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i<[allContacts count]; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
[myContacts addObject:firstName];
}
CFRelease(addressBook);
}
else
{
NSLog(@"Error");
}
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted )
{
NSLog(@"denied");
// Display an error.
}
}
NSLog(@"%@", myContacts);
Upvotes: 0
Views: 399
Reputation: 89509
Your code is almost completely correct. You simply need to print out your myContacts
array in the completion block of your Address Book request.
I've made some code changes for you:
__block NSMutableArray *myContacts = [[NSMutableArray alloc]init];
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL userDidGrantAddressBookAccess;
CFErrorRef addressBookError = NULL;
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized )
{
addressBook = ABAddressBookCreateWithOptions(NULL, &addressBookError);
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
userDidGrantAddressBookAccess = granted;
dispatch_semaphore_signal(sema);
if (addressBook!=nil)
{
NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i<[allContacts count]; i++)
{
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
// you need to do a nil check before adding a firstName to the array
//
// some contacts won't have first names, after all
if([firstName length] > 0)
{
[myContacts addObject:firstName];
}
}
CFRelease(addressBook);
}
else
{
NSLog(@"Error");
}
// *** HERE is where to print out the myContacts array***
NSLog(@"addresses are %@", myContacts);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else
{
if ( ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted )
{
NSLog(@"denied");
// Display an error.
}
}
// don't NSLog here, as myContacts array likey won't be filled out here...
// print it out in the completion block instead
// NSLog(@"%@", myContacts);
B.T.W., if your code is only going to run on iOS 9 and newer, consider using CNContactStore instead, as the AddressBook API's are deprecated and could eventually be removed from iOS.
Upvotes: 1