Programmatically Request Access to Contacts - iphone

Since updating to iOS 6 I've noticed that my code to add a contact to iPhone's address book no longer works. I believe this is a permission related problem, since Apple now requires user permission before accessing contacts (fixing this issue).
I expected the app to automatically ask permission to access contacts, like in the screenshot below, but it doesn't. Trying to add the contact just fails with ABAddressBookErrorDomain error 1.
Do I need to programmatically launch the access to contacts request dialog? How is that done?

As per this documentation on apple's site (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>
// 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
[self _addContactToAddressBook];
} 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
[self _addContactToAddressBook];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
}
Update For iOS 9 and later:
From Apple website :
Important
The Address Book UI framework is deprecated in iOS 9. Use the APIs defined in the ContactsUI framework instead. To learn more, see ContactsUI

That did the perfect trick for me!
On iOS6, apple introduce new privacy control, user can control the accessment of contact and calender by each app. So, in the code side, you need to add some way to request the permission. In iOS5 or before, we can always call
ABAddressBookRef addressBook = ABAddressBookCreate();
to get the addressbook without any problem, but in iOS6, if you don't have permission, this call will just return empty pointer. That why we need to change the method to get ABAddressBookRef.
__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) {
// Do whatever you want here.
}
In the code,semaphore is used for blocking until response, while ABAddressBookRequestAccessWithCompletion will ask for permission if the app didn't ask before. Otherwise it will just follow the settings in Settings-Privacy-Contact.
SOURCE: http://programmerjoe.blogspot.com/2012/10/ios6-permissions-contacts.html

For contacts framework:
- (void)checkPermissionForCNContacts
{
switch ([CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts])
{
case CNAuthorizationStatusNotDetermined:
{
[[[CNContactStore alloc] init] requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES)
[self showCNContactPicker];
}];
}
break;
case CNAuthorizationStatusRestricted:
case CNAuthorizationStatusDenied:
// Show custom alert
break;
case CNAuthorizationStatusAuthorized:
[self showCNContactPicker];
break;
}
}

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(self.isContactsChanged)
{
{
self.isContactsChanged=NO;
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, self);
int allPeopleCount = CFArrayGetCount(allPeople);
NSMutableArray *contactArrTemp = [[NSMutableArray alloc]init];
__block int noNumberCount=1;
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
newMoc = [[NSManagedObjectContext alloc] init];
[newMoc setPersistentStoreCoordinator:[[AppDelegate getAppDelegate] persistentStoreCoordinator]];
[self DeleteAllPhoneContact];
NSNotificationCenter *notify = [NSNotificationCenter defaultCenter];
[notify addObserver:self
selector:#selector(mergeChanges:)
name:NSManagedObjectContextDidSaveNotification
object:newMoc];
self.backgroundQueue = dispatch_queue_create("com.storephonecontacts.bgqueue", NULL);
__block NSMutableDictionary *dic;
__block NSString *strTime,*strName,*strMobile,*strEmail,*strNotes;
__block NSDate *nsDate;
dispatch_async(self.backgroundQueue, ^{
NSMutableDictionary *dict =nil;
for (int i = 0; i < allPeopleCount; i++)
{
dic = [[NSMutableDictionary alloc]init];
ABRecordRef record = CFArrayGetValueAtIndex(allPeople,i);
NSDate *date = (NSDate*)ABRecordCopyValue(record, kABPersonCreationDateProperty);
nsDate = [date retain];
NSDateFormatter *formatterTime = [[NSDateFormatter alloc] init];
[formatterTime setDateFormat:#"hh.mm"];
NSString *dateStrPhone = [formatterTime stringFromDate:date];
strTime = [dateStrPhone retain];
[formatterTime release];
NSString *name = (NSString*)ABRecordCopyValue(record, kABPersonFirstNameProperty);
if([name length]>0)
name = [name stringByAppendingString:#" "];
NSString *name1 = (NSString*)ABRecordCopyValue(record, kABPersonLastNameProperty);
if([name1 length]>0)
{
if([name length]>0)
name = [name stringByAppendingString:name1];
else
name = (NSString*)ABRecordCopyValue(record, kABPersonLastNameProperty);
}
if([name length]>0)
strName = [name retain];
else
strName = [#"noName" retain];
//to save notes
NSString *notes = (NSString*)ABRecordCopyValue(record, kABPersonNoteProperty);
if(notes == NULL){
strNotes = #"noNotes";
}
else{
strNotes = [notes retain];
}
//for image
if (!ABPersonHasImageData(record)){
}
else{
CFDataRef imageData = ABPersonCopyImageData(record);
UIImage *image = [UIImage imageWithData:(NSData *) imageData];
[dic setObject:image forKey:#"image"];
CFRelease(imageData);
}
//To set Mobile
NSMutableArray* mobileArray = [[NSMutableArray alloc] init];
ABMutableMultiValueRef multi = ABRecordCopyValue(record, kABPersonPhoneProperty);
if (ABMultiValueGetCount(multi) > 0) {
// collect all emails in array
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {
CFStringRef mobileRef = ABMultiValueCopyValueAtIndex(multi, i);
CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multi, i);
NSString *phoneLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
if([phoneLabel isEqualToString:#"mobile"])
[mobileArray addObject:(NSString *)mobileRef];
else if([phoneLabel isEqualToString:#"iPhone"])
[mobileArray addObject:(NSString *)mobileRef];
else if([phoneLabel isEqualToString:#"home"])
[mobileArray addObject:(NSString *)mobileRef];
else if([phoneLabel isEqualToString:#"work"])
[mobileArray addObject:(NSString *)mobileRef];
else if([phoneLabel isEqualToString:#"main"])
[mobileArray addObject:(NSString *)mobileRef];
else if([phoneLabel isEqualToString:#"other"])
[mobileArray addObject:(NSString *)mobileRef];
CFRelease(mobileRef);
CFRelease(locLabel);
}
}
CFRelease(multi);
if([mobileArray count]>0)
strMobile = [[mobileArray objectAtIndex:0]retain];
else{
NSString *str=[NSString stringWithFormat:#"noNumber%i",noNumberCount];
strMobile = [str retain];
noNumberCount++;
}
[mobileArray release];
//To set E-mail
NSMutableArray* emailArray = [[NSMutableArray alloc] init];
multi = ABRecordCopyValue(record, kABPersonEmailProperty);
if (ABMultiValueGetCount(multi) > 0) {
// collect all emails in array
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) {
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(multi, i);
[emailArray addObject:(NSString *)emailRef];
CFRelease(emailRef);
}
}
CFRelease(multi);
if([emailArray count]>0)
strEmail = [[emailArray objectAtIndex:0]retain];
else
strEmail = [#"noemail" retain];
[emailArray release];
bool addBool = NO;
if([strName isEqualToString:#"noName"]){
if([strEmail isEqualToString:#"noemail"]){
}
else{
[dic setObject:strEmail forKey:#"name"];
addBool = YES;
}
if(addBool == NO){
if([strMobile isEqualToString:#"noNumber"]){
}
else{
[dic setObject:strMobile forKey:#"name"];
addBool = YES;
}
}
}
else{
[dic setObject:strName forKey:#"name"];
addBool = YES;
}
[dic setObject:strEmail forKey:#"email"];
[dic setObject:strMobile forKey:#"mobile"];
[dic setObject:nsDate forKey:#"date"];
[dic setObject:strTime forKey:#"time"];
[dic setObject:strNotes forKey:#"notes"];
if(addBool == YES)
[contactArrTemp addObject:dic];
if([strMobile hasPrefix:#"0"]){
NSString *contactNumber=[strMobile stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:#""];
if(contactNumber.length>7)
[dic setObject:#"iPhone" forKey:#"ContactType"];
}
else {
if(strMobile.length>9)
[dic setObject:#"iPhone" forKey:#"ContactType"];
}
if(![[dic objectForKey:#"ContactType"] isKindOfClass:[NSNull class]] && [dic objectForKey:#"ContactType"])
{
[self InsertContactWithContactInfoDictionary:dic];
}
[strName release];
[nsDate release];
[strEmail release];
[strMobile release];
[strTime release];
[strNotes release];
[dic release];
}
dispatch_async(self.backgroundQueue, ^(void){ [self gcdDidFinishaddfebriteParsing:dict]; });
dispatch_release(self.backgroundQueue);
self.backgroundQueue=nil;
});
}
}
else
{
[[NSNotificationCenter defaultCenter] postNotificationName:#"PhoneContactsSaved" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:#"Successful"]];
[[NSNotificationCenter defaultCenter] postNotificationName:#"updateContacts" object:nil userInfo:[NSDictionary dictionaryWithObject:#"success" forKey:#"update"]];
}
}

Had some problems with yunas code on iOS6.1 in Xcode5. With some small adaptions it worked for me.
The problem was that ARC in iOS 6, did not allows dispatch_release(sema); Here's the working code. Note: I use m_addressbook instead of addressbook as ABAddressBookRef!
ViewController.m
#import "ViewController.h"
#import <AddressBook/AddressBook.h>
#import <AddressBook/ABAddressBook.h>
#import <AddressBook/ABPerson.h>
#interface ViewController ()
#property (nonatomic, strong) NSMutableArray* contactList;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
ABAddressBookRef m_addressbook = ABAddressBookCreateWithOptions(NULL, NULL);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
#autoreleasepool {
// Write your code here...
// Fetch data from SQLite DB
}
});
ABAddressBookRequestAccessWithCompletion(m_addressbook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
// do your stuff
}
}
// ...

- (void)viewDidLoad
{
[super viewDidLoad];
[self loadPhoneContacts];
}
-(void)loadPhoneContacts{
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied) {
// if you got here, user had previously denied/revoked permission for your
// app to access the contacts, and all you can do is handle this gracefully,
// perhaps telling the user that they have to go to settings to grant access
// to contacts
[[[UIAlertView alloc] initWithTitle:nil message:#"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
return;
}
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (error) {
NSLog(#"ABAddressBookCreateWithOptions error: %#", CFBridgingRelease(error));
if (addressBook) CFRelease(addressBook);
return;
}
if (status == kABAuthorizationStatusNotDetermined) {
// present the user the UI that requests permission to contacts ...
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (error) {
NSLog(#"ABAddressBookRequestAccessWithCompletion error: %#", CFBridgingRelease(error));
}
if (granted) {
// if they gave you permission, then just carry on
[self listPeopleInAddressBook:addressBook];
} else {
// however, if they didn't give you permission, handle it gracefully, for example...
dispatch_async(dispatch_get_main_queue(), ^{
// BTW, this is not on the main thread, so dispatch UI updates back to the main queue
[[[UIAlertView alloc] initWithTitle:nil message:#"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
});
}
if (addressBook) CFRelease(addressBook);
});
} else if (status == kABAuthorizationStatusAuthorized) {
[self listPeopleInAddressBook:addressBook];
if (addressBook) CFRelease(addressBook);
}
}
- (void)listPeopleInAddressBook:(ABAddressBookRef)addressBook
{
NSInteger numberOfPeople = ABAddressBookGetPersonCount(addressBook);
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
for (NSInteger i = 0; i < numberOfPeople; i++) {
ABRecordRef person = (__bridge ABRecordRef)allPeople[i];
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSLog(#"Name:%# %#", firstName, lastName);
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
CFIndex numberOfPhoneNumbers = ABMultiValueGetCount(phoneNumbers);
for (CFIndex i = 0; i < numberOfPhoneNumbers; i++) {
NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
NSLog(#" phone:%#", phoneNumber);
}
CFRelease(phoneNumbers);
NSLog(#"=============================================");
}
}

According to iOS recent apple development documentation, apple insists to use Contact besides AddressBook.
CNAuthorizationStatus authorizationStatus = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
switch (authorizationStatus) {
case CNAuthorizationStatusNotDetermined: {
break;
}
case CNAuthorizationStatusRestricted: {
break;
}
case CNAuthorizationStatusDenied:{
return;
}
break;
case CNAuthorizationStatusAuthorized: {
break;
}
}
Above objective c code helps to find weather contact access permission is granted or not.

If anyone have problem with addressBook in iOS5 then Use
ABAddressBookRef addressBook = ABAddressBookCreate();
Insted of
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,NULL);

ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!granted){
[[[UIAlertView alloc] initWithTitle:#"Contacts Access Denied"
message:#"This app requires access to your device's Contacts.\n\nPlease enable Contacts access for this app in Settings / Privacy / Contacts"
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil] show];
} else {
//access authorized
}
});
});
For add description to alert use in InfoPlist.strings.
NSContactsUsageDescription = "TESTING!";

Swift 3. Don't forget to import Contacts
func requestForContactAccess(completionHandler: #escaping (_ accessGranted: Bool) -> Void) {
let authorizationStatus = CNContactStore.authorizationStatus(for: CNEntityType.contacts)
switch authorizationStatus {
case .authorized:
completionHandler(true)
case .denied, .notDetermined:
self.contactStore.requestAccess(for: CNEntityType.contacts, completionHandler: { (access, accessError) -> Void in
if access {
completionHandler(access)
} else {
if authorizationStatus == CNAuthorizationStatus.denied {
DispatchQueue.main.async(execute: { () -> Void in
let message = "\(accessError!.localizedDescription)\n\nPlease allow the app to access your contacts through the Settings."
self.showMessage(message: message)
})
}
}
})
default:
completionHandler(false)
}
}

With iOS 6 you have to ask permission like this
requestAccessToEntityType:EKEntityTypeEvent completion:

To get Addressbook in ios
- (void)retreiveAllContacts
{
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
if (!people) {
return ;
}
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault,
CFArrayGetCount(people),
people);
CFArraySortValues(peopleMutable,
CFRangeMake(0, CFArrayGetCount(peopleMutable)),
(CFComparatorFunction) ABPersonComparePeopleByName,
(void*) ABPersonGetSortOrdering());
NSMutableArray *contacts = [[NSMutableArray alloc] initWithCapacity:CFArrayGetCount(peopleMutable)];
for (CFIndex i = 0; i < CFArrayGetCount(peopleMutable); i++)
{
ABRecordRef person = CFArrayGetValueAtIndex(peopleMutable, i);
int32_t recId = ABRecordGetRecordID(person);
NSString * abId = [NSString stringWithFormat:#"%d", recId];
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef emailIds = ABRecordCopyValue(person, kABPersonEmailProperty);
NSString* firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* lastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString* companyName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonOrganizationProperty);
NSString* displayName = [firstName ? firstName : #"" stringByAppendingFormat:#" %#", lastName ? lastName : #""];
displayName = [displayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" "]];
NSMutableDictionary* contactInfo = [[NSMutableDictionary alloc] init];
if(ABPersonHasImageData(person))
{
CFDataRef imageDataRef = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
NSData * imageData = (__bridge NSData *)imageDataRef;
UIImage * thumbImage = [UIImage imageWithData:imageData];
[contactInfo setObject:thumbImage forKey:#"picture"];
}
if(!firstName)
firstName = #"";
if (!lastName)
lastName = #"";
if(!displayName)
displayName = #"";
if(!companyName)
companyName = #"";
// [contactInfo setObject:[firstName capitalizedString] forKey:kFirstNameKey];
//[contactInfo setObject:[lastName capitalizedString] forKey:kLastNameKey];
[contactInfo setObject:[displayName capitalizedString] forKey:#"name"];
[contactInfo setObject:abId forKey:#"ABID"];
// [contactInfo setObject:companyName forKey:kCompanyNameKey];
NSMutableArray* phoneNumbersList = [[NSMutableArray alloc] init];
for (CFIndex j=0; j < ABMultiValueGetCount(phoneNumbers); j++)
{
NSString* phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, j);
CFStringRef localizedLabel = ABMultiValueCopyLabelAtIndex(phoneNumbers,j);
// NSString *phoneLabel =(__bridge NSString*) ABAddressBookCopyLocalizedLabel(localizedLabel);
if( phone)
{
// NSLog(#"validatedPhone: %#", validatedPhone);
[phoneNumbersList addObject:phone];
}
if (localizedLabel) {
// NSLog(#"localizedLabel: %#", localizedLabel);
CFRelease(localizedLabel);
}
}
if(phoneNumbers)
{
// NSLog(#"phoneNumbers: %#", phoneNumbers);
CFRelease(phoneNumbers);
// NSLog(#"phoneNumbers Release: %#", phoneNumbers);
}
[contactInfo setObject:phoneNumbersList forKey:#"phoneNumbers"];
NSMutableArray * emailList = [[NSMutableArray alloc] init];
for (CFIndex j=0; j < ABMultiValueGetCount(emailIds); j++)
{
NSString* email = (__bridge NSString*)ABMultiValueCopyValueAtIndex(emailIds, j);
CFStringRef localizedLabel = ABMultiValueCopyLabelAtIndex(emailIds, j);
if(email)
{
[emailList addObject:email];
}
}
if(emailIds)
{
CFRelease(emailIds);
}
if(emailList && [emailList count])
[contactInfo setObject:emailList forKey:#"emails"];
if ([phoneNumbersList count] > 0 || [emailList count] > 0) {
[contacts addObject:contactInfo];
}
}
//CFRelease();
CFRelease(people);
if([contacts count])
{
[self createiOSContactsDataSourceWithFeed:contacts];
}
}

This code shows how to set the permission and how to get all the contacts from the phone and show the contacts in the list with the label tag
var contactStore = CNContactStore()
var contactArray = [CNContact]()
func getContacts()
{
if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined
{
contactStore.requestAccess(for: .contacts, completionHandler: { (authorized:Bool, error:Error?) in
if authorized {
let requestForContacts = CNContactFetchRequest(keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: CNContactFormatterStyle.fullName), CNContactPhoneNumbersKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor, CNContactNicknameKey as CNKeyDescriptor])
do{
try self.contactStore.enumerateContacts(with: requestForContacts) { (contacts : CNContact, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
self.contactArray.append(contacts)
//print("hello")
}
}
catch {
print("EXCEPTION COUGHT")
}
}
})
}
else if CNContactStore.authorizationStatus(for: .contacts) == .authorized
{
let requestForContacts = CNContactFetchRequest(keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: CNContactFormatterStyle.fullName), CNContactPhoneNumbersKey as CNKeyDescriptor, CNContactImageDataKey as CNKeyDescriptor, CNContactNicknameKey as CNKeyDescriptor])
do{
try self.contactStore.enumerateContacts(with: requestForContacts) { (contacts : CNContact, stop: UnsafeMutablePointer<ObjCBool>) -> Void in
self.contactArray.append(contacts)
}
}
catch {
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
getContacts()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
//print(contactArray)
return contactArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "defaultCell")
if cell != nil{
//var dig = String()
var hmdig = [String]()
let names = contactArray[indexPath.row]
print(names)
let name1 = names.givenName+" "+names.middleName+" "+names.familyName
for number in names.phoneNumbers
{
let phoneNumber = number.value
let dig = (phoneNumber.value(forKey: "digits") as? String)!
hmdig.append(dig)
}
// Set the contact image.
if let imageData = names.imageData
{
let myImage = cell?.viewWithTag(30) as! UIImageView
myImage.image = UIImage(data: imageData)
}
// let niknm = names.nickname
let nameLable1 = cell?.viewWithTag(10) as! UILabel
nameLable1.text = name1
let nameLable2 = cell?.viewWithTag(20) as? UILabel
nameLable2?.text = hmdig.joined(separator: ",\n")
// let nameLable3 = cell?.viewWithTag(40) as? UILabel
// nameLable3?.text = niknm
return cell!
}
else{
return UITableViewCell()
}
}

Related

Import ios contacts in a tableview [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I searched the internet for importing iOS contacts in a UITableViewController because I have a UITableViewController as a root for a UINavigationController and I called it contacts like whatsapp does and what I have found is how to import the contacts picker with a modal segue and I dont want that I want to import them in a custom cell and edit on them . So how can I do that ?
Try this function this is working for me.
-(NSArray *)getAllContacts
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__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);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
#ifdef DEBUG
NSLog(#"Fetching contact info ----> ");
#endif
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
for (int i = 0; i < nPeople; i++)
{
ContactsData *contacts = [ContactsData new];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
contacts.firstNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
contacts.lastNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (!contacts.firstNames) {
contacts.firstNames = #"";
}
if (!contacts.lastNames) {
contacts.lastNames = #"";
}
// get contacts picture, if pic doesn't exists, show standart one
NSData *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
contacts.image = [UIImage imageWithData:imgData];
if (!contacts.image) {
contacts.image = [UIImage imageNamed:#"NOIMG.png"];
}
//get Phone Numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
if (ABMultiValueGetCount(multiPhones)<0)
{
contacts.phonenumber=#"";
}
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
contacts.phonenumber=phoneNumber;
[phoneNumbers addObject:phoneNumber];
NSLog(#"All numbers %#", phoneNumbers);
}
[contacts setNumbers:phoneNumbers];
//get Contact email
NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge NSString *)contactEmailRef;
[contactEmails addObject:contactEmail];
// NSLog(#"All emails are:%#", contactEmails);
}
[contacts setEmails:contactEmails];
[items addObject:contacts];
[Contactarray addObject:contacts];
#ifdef DEBUG
NSLog(#"Person is: %#", contacts.firstNames);
// NSLog(#"Phones are: %#", contacts.numbers);
// NSLog(#"Email is:%#", contacts.emails);
#endif
}
NSLog(#"%#",items);
return items;
} else {
#ifdef DEBUG
NSLog(#"Cannot fetch Contacts :( ");
#endif
return NO;
}
}
i hope this code useful for you .

Getting various memory leaks while accessing AddressBook in iOS 7

I am using using this function to access the contacts list and saving the contact object using MContact class,but this code is giving me several memory leaks,i am not able to figure this out .Please help!
+(NSArray *)getAllContacts
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__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);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
#ifdef DEBUG
NSLog(#"Fetching contact info ----> ");
#endif
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
for (int i = 0; i < nPeople; i++)
{
MContact *contacts = [MContact new];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
contacts.firstName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
contacts.lastName = (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (!contacts.firstName) {
contacts.firstName = #"";
}
if (!contacts.lastName) {
contacts.lastName = #"";
}
NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge NSString *)contactEmailRef;
[contactEmails addObject:contactEmail];
// NSLog(#"All emails are:%#", contactEmails);
}
if([contactEmails count]==0){
return items ;
}
else{
[contacts setemails:contactEmails];
[items addObject:contacts];
}
#ifdef DEBUG
#endif
}
CFRelease(addressBook);
return items;
} else {
#ifdef DEBUG
NSLog(#"Cannot fetch Contacts :( ");
#endif
return NO;
}
}
The thing you need to watch out for here is that, by convention, functions that "copy" return objects with incremented reference counts, just like functions that "create". So, the way you've CFReleased your addressBook, you need to do that with the copied objects too. Alternatively, when you bridge your CFObjects to NSObjects, a __bridge_transfer would pass ownership to ARC, so ARC can release it for you.
The other gotcha here is the multiple return points in the function. If that's what you want to do, you need to make sure that appropriate things are cleaned up before each return.
Hopefully I've caught everything here:
+ (NSArray *)getAllContacts
{
CFErrorRef *error = nil;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__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);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
#ifdef DEBUG
NSLog(#"Fetching contact info ----> ");
#endif
//ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); Removing this line as you have this already. If you need to recreate, you must release the first one.
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
CFRelease(source); // Copied object, so release.
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
for (int i = 0; i < nPeople; i++)
{
MContact *contacts = [MContact new];
ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
//get First Name and Last Name
contacts.firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
contacts.lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
if (!contacts.firstName) {
contacts.firstName = #"";
}
if (!contacts.lastName) {
contacts.lastName = #"";
}
NSMutableArray *contactEmails = [NSMutableArray new];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
NSString *contactEmail = (__bridge_transfer NSString *)contactEmailRef;
[contactEmails addObject:contactEmail];
// NSLog(#"All emails are:%#", contactEmails);
}
CFRelease(multiEmails);
if([contactEmails count]==0) {
break;
// If we return here, we'll leak addressBook and allPeople
// We could release both, then return.
}
else {
[contacts setemails:contactEmails];
[items addObject:contacts];
}
#ifdef DEBUG
#endif
}
CFRelease(addressBook);
CFRelease(allPeople); // Copied, so must release
return items;
} else {
#ifdef DEBUG
NSLog(#"Cannot fetch Contacts :( ");
#endif
CFRelease(addressBook);
return nil;
}
}

I fetch contact in addressbook iphone 4s not working

+(NSMutableArray *)getLastName
{
ABAddressBookRef addressBook = ABAddressBookCreate();
NSMutableArray *lastNameArray = [[NSMutableArray alloc]init];
ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
CFIndex numberOfContacts = ABAddressBookGetPersonCount(addressBook);
NSLog(#"%ld",numberOfContacts);
CFArrayRef people = (__bridge CFArrayRef)((__bridge NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName));
NSArray * tempArray = [[NSArray alloc]init];
tempArray = (__bridge NSArray *)(people);
if ([tempArray count]>0) {
for (int personIndex = 0; personIndex < [tempArray count]-1; personIndex++) {
ABRecordRef person = (__bridge ABRecordRef)([tempArray objectAtIndex:personIndex]);
ABMultiValueRef lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *lastNameString = (__bridge NSString *)lastName;
if (lastName!=nil)
[lastNameArray addObject:lastNameString];
}
}
return lastNameArray;
}
I am using the xcode 6.0. iphone 3s is working fine but iphone 4s and iphone 5 not working in that code
-(void)_addContactToAddressBook
{
char *lastNameString, *firstNameString,*emailString;
NSMutableArray *arrname =[[NSMutableArray alloc] init];
NSMutableArray *arrTemp = [[NSMutableArray alloc] init];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
// AddressBook
ABAddressBookRef ab = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(ab);
CFIndex nPeople = ABAddressBookGetPersonCount(ab);
for(int i = 0; i < nPeople; i++)
{
[dict removeAllObjects];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
ABMutableMultiValueRef multi = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFStringRef email;
if (ABMultiValueGetCount(multi) > 0) {
// collect all emails in array
for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++)
{
CFStringRef emailRef = ABMultiValueCopyValueAtIndex(multi, i);
//[personDealingWithEmails addObject:(NSString *)emailRef];
email=emailRef;
CFRelease(emailRef);
}
}
else
{
email=nil;
}
CFDataRef imgData;
if(ABPersonHasImageData(ref))
{
imgData = ABPersonCopyImageData(ref);
}else {
imgData=nil;
}
static char* fallback = "";
int fbLength = strlen(fallback);
int firstNameLength = fbLength;
bool firstNameFallback = true;
int lastNameLength = fbLength;
bool lastNameFallback = true;
int emailLength = fbLength;
bool emailFallback = true;
if (firstName != NULL)
{
firstNameLength = (int) CFStringGetLength(firstName);
firstNameFallback = false;
}
if (lastName != NULL)
{
lastNameLength = (int) CFStringGetLength(lastName);
lastNameFallback = false;
}
if (email != NULL)
{
emailLength = (int) CFStringGetLength(email);
emailFallback = false;
}
if (firstNameLength == 0)
{
firstNameLength = fbLength;
firstNameFallback = true;
}
if (lastNameLength == 0)
{
lastNameLength = fbLength;
lastNameFallback = true;
}
if (emailLength == 0)
{
emailLength = fbLength;
emailFallback = true;
}
firstNameString = malloc(sizeof(char)*(firstNameLength+1));
lastNameString = malloc(sizeof(char)*(lastNameLength+1));
emailString = malloc(sizeof(char)*(emailLength+1));
if (firstNameFallback == true)
{
strcpy(firstNameString, fallback);
}
else
{
CFStringGetCString(firstName, firstNameString, 10*CFStringGetLength(firstName), kCFStringEncodingASCII);
}
if (lastNameFallback == true)
{
strcpy(lastNameString, fallback);
}
else
{
CFStringGetCString(lastName, lastNameString, 10*CFStringGetLength(lastName), kCFStringEncodingASCII);
}
if (emailFallback == true)
{
strcpy(emailString, fallback);
}
else
{
CFStringGetCString(email, emailString, 10*CFStringGetLength(email), kCFStringEncodingASCII);
}
printf("%d.\t%s %s\n", i, firstNameString, lastNameString);
NSString *fname= [NSString stringWithFormat:#"%s",firstNameString];
NSString *lname= [NSString stringWithFormat:#"%s",lastNameString];
NSString *fullName=[NSString stringWithFormat:#"%#%#%#",fname,([fname length]!=0)?#" ":#"",lname];
NSString *eMail= [NSString stringWithFormat:#"%s",emailString];
NSData *myData;
if (imgData) {
myData=(__bridge NSData *)imgData;
}else {
myData=nil;
}
//fetch Phone num
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSArray* phoneNumbers = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
CFRelease(phoneNumberProperty);
// Do whatever you want with the phone numbers
//NSLog(#"Phone numbers = %#", phoneNumbers);
NSString *PhoneNum = [phoneNumbers objectAtIndex:0];
//--------------remove special char form string(Phone number)-----------------
NSString *originalString = PhoneNum;
//NSLog(#"%#", originalString);
NSMutableString *strippedString = [NSMutableString
stringWithCapacity:originalString.length];
NSScanner *scanner = [NSScanner scannerWithString:originalString];
NSCharacterSet *numbers = [NSCharacterSet
characterSetWithCharactersInString:#"0123456789"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:numbers intoString:&buffer]) {
[strippedString appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
if([fname isEqualToString:#""] && [lname isEqualToString:#""]){
}else{
if (myData) {
UIImage *img = [UIImage imageWithData:myData];
[dict setObject:img forKey:#"imgData"];
}
[dict setValue:fname forKey:#"fname"];
[dict setValue:lname forKey:#"lname"];
[dict setValue:fullName forKey:#"fullName"];
[dict setValue:eMail forKey:#"email"];
[dict setValue:strippedString forKey:#"phoneNumber"];
[dict setValue:#"addressbook" forKey:#"type"];
[arrname addObject:[dict copy]];
}
if (firstName != NULL)
{
CFRelease(firstName);
}
if (imgData != NULL)
{
CFRelease(imgData);
}
if (lastName != NULL)
{
CFRelease(lastName);
}
if (email != NULL)
{
CFRelease(email);
}
free(firstNameString);
free(lastNameString);
free(emailString);
}
for (int i=0; i<[arrname count]; i++)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:#"0" forKey:#"sel"];
[arrTemp addObject:dict];
}
[arrallcontacts addObjectsFromArray:arrname];
[arraddedcontactsfulllist addObjectsFromArray:arrallcontacts];
NSSortDescriptor *sortByfullName = [[NSSortDescriptor alloc] initWithKey:#"fullName" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:sortByfullName];
NSArray *sorted = [arraddedcontactsfulllist sortedArrayUsingDescriptors:descriptors];
[arraddedcontactsfulllist removeAllObjects];
[arraddedcontactsfulllist addObjectsFromArray:sorted];
[arraddedcontacts removeAllObjects];
[arraddedcontacts addObjectsFromArray:sorted];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
tblview.alpha=1;
[UIView commitAnimations];
[tblview reloadData];
if (![[[[NSUserDefaults standardUserDefaults] objectForKey:#"USER_DETAIL"] objectForKey:#"facebook"] isEqualToString:#"Unauthenticate"]) {
[UIApplication sharedApplication].networkActivityIndicatorVisible=YES;
[self performSelector:#selector(getfacebookfriends) withObject:nil afterDelay:0.0001];
}
}
-(void)fetchIphoneContact
{
if ([[UIDevice currentDevice].systemVersion floatValue]>=6.0)
{
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
// First time access has been granted, add the contact
[self _addContactToAddressBook];
[tblview reloadData];
});
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
// The user has previously given access, add the contact
[self _addContactToAddressBook];
[tblview reloadData];
}
else {
// The user has previously denied access
// Send an alert telling user to change privacy setting in settings app
DisplayAlertWithTitle(APP_Name, #"You can change your privacy setting in settings app");
}
}
else
{
[self _addContactToAddressBook];
}
}
This will work on iOS6 and lower..
I think the problem is with you iOS version.... If you're using iOS 6.0 then ABAddressBookCreate() won't work... it is fine for iOS 5.1 or before.... so you can do like,
ABAddressBookRef addressBook;
if ([self isABAddressBookCreateWithOptionsAvailable])
{
// iOS 6
CFErrorRef error = nil;
addressBook = ABAddressBookCreateWithOptions(NULL,&error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
{
});
ABAddressBookRevert(addressBook);
}
else
{
// iOS 4/5
addressBook = ABAddressBookCreate();
}
-(BOOL)isABAddressBookCreateWithOptionsAvailable
{
return &ABAddressBookCreateWithOptions != NULL;
}
By this you can get all contacts...:)
There could be different ways to retrieve AddressBook in IOS but one way which I use to retrieve addressbook is as follows, make sure you are using ABPeoplePickerNavigationControllerDelegate You can open the AddressBook Controller on a button click like this
-(IBAction)getContact:(id)sender {
// creating the picker
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
// place the delegate of the picker to the controll
picker.peoplePickerDelegate = self;
[self presentModalViewController:picker animated:YES];
}
after addressBook is presented you can use the delegate methods to receive the contact information as follows
- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
// setting the first name
firstNameTxt.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
// setting the last name
lastNameTxt.text = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
// setting the number
/*
this function will set the first number it finds
if you do not set a number for a contact it will probably
crash
*/
ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
telefonTxt.text = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
ABMultiValueRef multi1 = ABRecordCopyValue(person, kABPersonEmailProperty);
emailTxt.text = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi1, 0);
// remove the controller
[self dismissModalViewControllerAnimated:YES];
return NO;
}

Accessing all the email IDs for each contact from the iphone contactlist through code

I am new to iphone.presently i am working in project but i have struck in the middle of the project because of don't know how to access all the email id's for each contact from the iphone contactlist through coding.if anybody knows about this concept can you please help me....
this code gives u email along with their display names and contact name too
-(NSMutableArray*)getEmailAndPhoneOfPhoneContacts
{
NSLog(#"getPhoneContacts");
NSAutoreleasePool *pool= [[NSAutoreleasePool alloc] init];
NSMutableArray *tempArray=[[NSMutableArray alloc] init];
ABAddressBookRef iPhoneAddressBook=ABAddressBookCreate();
if(!iPhoneAddressBook)
{
DLog(#"unable to open addressBook");
}
//ABPerson *newPerson;
NSAutoreleasePool *innerPool=nil;
CFArrayRef allPeople=ABAddressBookCopyArrayOfAllPeople(iPhoneAddressBook);
NSMutableArray *peopleArray=[NSMutableArray arrayWithArray:(NSMutableArray*)allPeople];
BOOL shouldReleasePool=NO;
NSInteger i;
for(i=0;i<[peopleArray count];i++)
{
if((i&255)==0)
{
innerPool= [[NSAutoreleasePool alloc] init];
shouldReleasePool=YES;
}
ABRecordRef record=[peopleArray objectAtIndex:i];
Contact *objPhoneContact=[[Contact alloc] init];
objPhoneContact.contactType=STATIC_CONTACT;
CFStringRef prefixName=ABRecordCopyValue(record, kABPersonPrefixProperty);
CFStringRef firstName=ABRecordCopyValue(record, kABPersonFirstNameProperty);
CFStringRef middleName=ABRecordCopyValue(record, kABPersonMiddleNamePhoneticProperty);
CFStringRef lastName=ABRecordCopyValue(record, kABPersonLastNameProperty);
CFStringRef suffixName=ABRecordCopyValue(record, kABPersonSuffixProperty);
NSMutableString *contactname =[[NSMutableString alloc] init];
// concating all the names
if (prefixName) {
[contactname appendString:[NSString stringWithString:(NSString*)prefixName]];
CFRelease(prefixName);
}
if (firstName) {
[contactname appendString:[NSString stringWithString:(NSString*)firstName]];
CFRelease(firstName);
}
if (middleName) {
[contactname appendString:[NSString stringWithString:(NSString*)middleName]];
CFRelease(middleName);
}
if (lastName) {
[contactname appendString:[NSString stringWithString:(NSString*)lastName]];
CFRelease(lastName);
}
if (suffixName) {
[contactname appendString:[NSString stringWithString:(NSString*)suffixName]];
CFRelease(suffixName);
}
// if emty then get the organization property
if (contactname == nil || [contactname length]<1) {
CFStringRef orgName=ABRecordCopyValue(record, kABPersonOrganizationProperty);
if (orgName) {
[contactname appendString:[NSString stringWithString:(NSString*)orgName]];
CFRelease(orgName);
}
}
//if still empty then assign (no name) to it
if (contactname == nil || [contactname length]<1)
[contactname setString:#"(no name)"];
objPhoneContact.mContactName = [contactname stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
[contactname release];
contactname = nil;
ABMutableMultiValueRef multi;
int multiCount=0;
multi=ABRecordCopyValue(record,kABPersonEmailProperty);
NSUInteger emailIndex_home=301;
NSUInteger emailIndex_work=321;
NSUInteger emailIndex_other=341; //~400
multiCount=ABMultiValueGetCount(multi);
if(multiCount ==0)
{
//objPhoneContact.mEmailId=#"";
}
else
{
for( int i=0; i < multiCount; i++)
{
ContactProperty* objEmailContactProperty=[[ContactProperty alloc] init];
objEmailContactProperty.mContactPropertyString=(NSString*)ABMultiValueCopyValueAtIndex(multi, i);
objEmailContactProperty.mDisplayName=(NSString*)ABMultiValueCopyLabelAtIndex(multi, i);
objEmailContactProperty.mDisplayName=[objEmailContactProperty.mDisplayName stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"_$!<>"]];
if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:#"home"]==NSOrderedSame)
{
objEmailContactProperty.mContactPropId=emailIndex_home;
emailIndex_home++;
}
else if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:#"work"]==NSOrderedSame)
{
objEmailContactProperty.mContactPropId=emailIndex_work;
emailIndex_work++;
}
/*
else if([objEmailContactProperty.mDisplayName caseInsensitiveCompare:#"other"]==NSOrderedSame)
{
objEmailContactProperty.mContactPropId=[NSString stringWithFormat:#"%d",emailIndex_other];
emailIndex_other++;
}
*/
else
{
objEmailContactProperty.mContactPropId=emailIndex_other;
emailIndex_other++;
}
objEmailContactProperty.mContactDataType=#"Email";
[objPhoneContact.mPropertyArray addObject:objEmailContactProperty];
[objEmailContactProperty release];
}
}
if(multi)
CFRelease(multi);
[tempArray addObject:objPhoneContact];
[objPhoneContact release];
if(shouldReleasePool)
{
[innerPool drain];
shouldReleasePool=NO;
}
}
self.mPhoneContactArray=tempArray;
CFRelease(iPhoneAddressBook);
CFRelease(allPeople);
[pool drain];
return [tempArray autorelease];
}
Make sure you import these two files:
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
-(IBAction)showPeoplePickerController
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
[menuArray removeAllObjects];
if(menuArray ==nil)
menuArray = [[NSMutableArray alloc] init];
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *localDic=[[NSMutableDictionary alloc]init];
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
ABRecordID recordId = ABRecordGetRecordID(ref);
[localDic setObject:[NSString stringWithFormat:#"%d",recordId] forKey:#"Record_Id"];
//get firstname
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
[localDic setObject:[NSString stringWithFormat:#"%#",firstName] forKey:#"first_name"];
//get lastname
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
[localDic setObject:[NSString stringWithFormat:#"%#",lastName] forKey:#"last_name"];
NSString *contactFirstLast = [NSString stringWithFormat: #"%# %#",firstName,lastName];
[localDic setObject:contactFirstLast forKey:#"FullName"];
//get EmailIds
ABMutableMultiValueRef EmailIds = ABRecordCopyValue(ref, kABPersonEmailProperty);
CFTypeRef EmailId;
NSString *EmailLabel;
for (CFIndex i = 0; i < ABMultiValueGetCount(EmailIds); i++) {
EmailLabel=[NSString stringWithFormat:#"%#",ABMultiValueCopyLabelAtIndex(EmailIds,i)];
if([EmailLabel isEqualToString:#"_$!<Home>!$_"])
{
EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i);
[localDic setObject:[NSString stringWithFormat:#"%#",EmailId] forKey:#"Email_Home"];
}
else if([EmailLabel isEqualToString:#"_$!<Work>!$_"])
{
EmailId = ABMultiValueCopyValueAtIndex(EmailIds,i);
[localDic setObject:[NSString stringWithFormat:#"%#",EmailId] forKey:#"Email_Work"];
break;
}
}
[menuArray addObject:localDic];
}
NSLog(#"%#",menuArray);
}
In this way you get all EmailIds of each people.
and also add these two frameworks in your project:
AddressBook.framework
AddressBookUI.framework

Possible ways to export IPhone addressbook db

I want to export IPhone's AddressBook.sqlitedb into my IPhone application.
I have searched around the net but everything seems to iterate over the "ABAddressBook"
but i want to know that is it possible to export IPhone's AddressBook.sqlitedb into my IPhone application programmatically?
Please let me know any valuable comments!!!
Thanks for your help.....
you have to fetch every single value and then insert it into your DB.
Here is the code through which i got iphone address book into my application's Db.
just call the below method where ever you want to get iphone Address Book but i'll suggest you to call this methos in delegat.m method- DidFinishLanching:
-(void)fetchRecordsFromAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
//NSArray *addresses = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
//[arrayContacts removeAllObjects];
[self emptyDataContext];
for (int i = 0; i < nPeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
////////////////// get first name ///////////////////
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
CFStringRef nickName = ABRecordCopyValue(ref, kABPersonNicknameProperty);
CFStringRef middleName = ABRecordCopyValue(ref, kABPersonMiddleNameProperty);
////////////////// get image ///////////////////
// ABMultiValueRef ContactImage = (ABMultiValueRef) ABRecordCopyValue(ref,kABPersonImageFormatThumbnail);
NSData *data=nil;
// NSLog(#"Image Testing is : %#",ref);
if(ABPersonHasImageData(ref))
{
data = [(NSData *) ABPersonCopyImageData(ref) autorelease];
if(data)
{
// NSLog(#"Im Testing is : %#",data);
//image = [[UIImage alloc] initWithData:data];
}
}
// NSLog(#"Im agte is : %#",ContactImage);
// NSLog(#" Name is : %#",firstName);
////////////////// get email ///////////////////
ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonEmailProperty);
NSString *emailID=#"";
if(ABMultiValueGetCount(emails)>=1)
{
emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails,0);
}
////////////////// get phone number ///////////////////
ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSString *phone=#"";
NSString *homeNumber = #"";
NSString *worknumber = #"";
if(ABMultiValueGetCount(phones)>=1)
{
//int ph = [ABMultiValueCopyValueAtIndex(phones, 0) intValue];
phone = (NSString *)ABMultiValueCopyValueAtIndex(phones,0);
}
// NSLog(#"%#",(NSString*)phone);
if(ABMultiValueGetCount(phones)>=2)
{
homeNumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,1);
}
if(ABMultiValueGetCount(phones)>=3)
{
worknumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,2);
}
NSMutableArray *arrayContacts = [[NSMutableArray alloc] init ];
///////////////////////////// insert into array ////////////////////////////
arrayContacts = [CoreDataAPIMethods getObjectsFromContext:#"AllContactData" :#"Index" :NO :self.managedObjectContext];
//////////////////////////// insert Index ///////////////////////////////
int NewEntryID;
if ([arrayContacts count] > 0)
{
AllContactData * Contacdata = [arrayContacts objectAtIndex:0];
NewEntryID = [Contacdata.Index intValue] +1;
}
else
{
NewEntryID = 1;
}
NSString *capitalisedSentence =
[(NSString *)firstName stringByReplacingCharactersInRange:NSMakeRange(0,1)
withString:[[(NSString *)firstName substringToIndex:1] capitalizedString]];
AllContactData *Contactitem=(AllContactData *)[NSEntityDescription insertNewObjectForEntityForName:#"AllContactData" inManagedObjectContext:self.managedObjectContext];
// NSLog(#"%#",capitalisedSentence);
Contactitem.Name = capitalisedSentence;
Contactitem.LastName = (NSString*)lastName;
Contactitem.NickName = (NSString*)nickName;
Contactitem.MiddleName = (NSString*)middleName;
Contactitem.Email=(NSString*)emailID;
phone = [phone stringByReplacingOccurrencesOfString:#"(" withString:#""];
phone = [phone stringByReplacingOccurrencesOfString:#")" withString:#""];
phone = [phone stringByReplacingOccurrencesOfString:#"+" withString:#""];
phone = [phone stringByReplacingOccurrencesOfString:#" " withString:#""];
phone = [phone stringByReplacingOccurrencesOfString:#"-" withString:#""];
NSLog(#"The Replacing Stinr is : %#", phone);
Contactitem.PhoneNumber=(NSString*)phone;
Contactitem.HomeNumber=(NSString*)homeNumber;
Contactitem.WorkNumber=(NSString*)worknumber;
Contactitem.Index = [NSNumber numberWithInt:NewEntryID];
Contactitem.Image = data;
// NSLog(#"Image in databse is : %#",(NSData *)ContactImage);
if(firstName!=nil)
{
CFRelease(firstName);
}
CFRelease(ref);
}
CFRelease(allPeople);
///////////////////////////// save entries ////////////////////////////
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error...
}
}
-(void)emptyDataContext
{
self.managedObjectContext = [(Dial_Up_AppAppDelegate*)[UIApplication sharedApplication].delegate managedObjectContext];
NSLog(#"managedObjectContext=%#",self.managedObjectContext);
// Get all counties, It's the top level object and the reference cascade deletion downward
NSMutableArray* mutableFetchResults = [CoreDataAPIMethods getObjectsFromContext:#"AllContactData" :#"Name" :YES :self.managedObjectContext];
// Delete all Counties
for (int i = 0; i < [mutableFetchResults count]; i++) {
[managedObjectContext deleteObject:[mutableFetchResults objectAtIndex:i]];
}
//[mutableFetchResults release];
// Update the data model effectivly removing the objects we removed above.
NSError *error;
if([mutableFetchResults count] > 0)
{
if (![managedObjectContext save:&error]) {
// Handle the error.
//NSLog([error domain]);
}
}
}
Hope this will help you.