I want to link the address book view controller in my app , I have searched the internet and found in apple developer guide but they show how to open contacts picker when clicking on a button and I don't want that I want the contacts picker be a view controller in my storyboard . I think i can do that by making a custom tableviewcontroller and getting information from address book or can I do it directly ?
Asked
Active
Viewed 163 times
1 Answers
2
This should help you get started:
Also, don't forget to import and link to the following frameworks:
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
Simply call: [self requestPermissionForContacts];
- (void)requestPermissionForContacts
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error)
{
if (granted)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self findContacts];
});
}
else
{
dispatch_async(dispatch_get_main_queue(), ^
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Find Contacts" message:@"To allow us to find your contacts, you will need to go to the Settings app > Privacy > Contacts, and set your app name here to On." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});
}
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
[self findContacts];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Find Contacts" message:@"To allow us to find your contacts, you will need to go to the Settings app > Privacy > Contacts, and set your app name here to On." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
- (void)findContacts
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
CFIndex numberOfPeople = CFArrayGetCount(people);
if (numberOfPeople > 0)
{
NSMutableArray *mutableEmailsArray = [[NSMutableArray alloc]init];
NSMutableArray *mutablePhonesArray = [[NSMutableArray alloc]init];
for (int i = 0; i < numberOfPeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(people, i);
ABMultiValueRef emails = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonEmailProperty));
ABMultiValueRef phones = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
if (ABMultiValueGetCount(emails) > 0)
{
[mutableEmailsArray addObjectsFromArray:((__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues(emails)))];
}
if (ABMultiValueGetCount(phones) > 0)
{
[mutablePhonesArray addObjectsFromArray:((__bridge NSArray *)(ABMultiValueCopyArrayOfAllValues(phones)))];
}
}
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:[NSString stringWithFormat:@"%ld Emails %ld Phone Numbers", (unsigned long)mutableEmailsArray.count, (unsigned long)mutablePhonesArray.count] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Contacts" message:@"No contacts were found on your device." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}

klcjr89
- 5,862
- 10
- 58
- 91
-
You cant expect to copy and paste code and expect it to work. You need to LINK and import the frameworks I listed above – klcjr89 Mar 24 '14 at 18:03
-
i added them ... the errors was that ( findcontacts ) selector is not found ? – user3380361 Mar 24 '14 at 18:12
-
sorry i didn't copy all the code ... now no errors found ... but it doesn't view my contacts – user3380361 Mar 24 '14 at 18:17
-
It's not supposed to. It's your job to work with the code I provided to extract the NSString's you need from the `ABRecordRef` – klcjr89 Mar 24 '14 at 18:18
-
and where i must call ( self requestPermissionForContacts]; ) – user3380361 Mar 24 '14 at 18:21
-
Call it whereever you're wanting to show/find contacts – klcjr89 Mar 24 '14 at 18:23
-
oki DMContactsController.m:17:1: Expected identifier or '(' DMContactsController.m:17:2: Use of undeclared identifier 'self' – user3380361 Mar 24 '14 at 18:42
-
Hmm, I'm not sure. Post your .h and .m files in your original question above as an edit and I'll take a look. – klcjr89 Mar 24 '14 at 18:56