Get iPhone's contacts list in Objective-C - contacts

I'd like to get my iPhone's contacts list without using the iOS Navigation Picker.
Here's the code I wrote...but it's not working, do you know why?
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
CFArrayRef allPeopleRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, nil, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for(int i=0; i<nPeople; i++){
ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleRef, i );
CFStringRef tmpStringRef = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(#"FirstName: %#", (__bridge NSString *)tmpStringRef);
CFRelease(tmpStringRef);
}
CFRelease(allPeopleRef);
CFRelease(addressBook);

Related

how to edit a phone number values programmatically from address book ios

I'm trying to replace an specific phone number for an specific contact programmatically in iOS, taking the contacts form address book.
I don't know why I can't save the new phone number and refresh the address book to show the change.
I'm doing this:
+(BOOL) changeContactPhoneNumber:(NSString *) phoneSought
forThis:(NSString *) newPhoneNumber{
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef contactSelected;
CFStringRef mobileLabelNumber;
CFErrorRef error = nil;
// Do whatever you want here.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i = 0; i < nPeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSString* mobilePhoneNumber=#"";
if (ABMultiValueGetCount(phones) > 0) {
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
[mobilePhoneNumber release];
mobilePhoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
if([mobilePhoneNumber isEqualToString:phoneSought]){
contactSelected = ref;
mobileLabelNumber = ABMultiValueCopyLabelAtIndex(phones, i);
}
}
}
}
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
bool didAddPhone = ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)newPhoneNumber,mobileLabelNumber, NULL);
if(didAddPhone){
ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected),
kABPersonPhoneProperty,
phoneNumberMultiValue,
nil);
bool bSuccess = ABAddressBookSave(addressBook, &error);
if (!bSuccess) {
NSLog(#"Could not save to address book: %#", error);
} else {
return YES;
}
} else {
NSLog(#"Error editing phone number: %#", error);
error = nil;
}
return NO;
}
You should debug your code and try to figure out whether the format of the phone numbers you are providing to the method are matching or not.
For e.g. when i am logging my contact list phone numbers these are results
Number...555-478-7672
Number...(408) 439-5270
Number...(408) 555-3514
Number...888-555-5512
Number...888-555-1212
Number...555-522-8243
Number...(555) 766-4823
Number...(707) 555-1854
Number...555-610-6679
And i was comparing these number against unformatted number string.
Secondly
ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected),
kABPersonPhoneProperty,
phoneNumberMultiValue,
nil);
Whose actual declaration is
ABRecordSetValue(ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef* error);
Although ABAddressBookGetPersonWithRecordID returns a ABRecordRef but you already have ABRecordRef contactSelected; so in my view you should use
ABRecordSetValue(contactSelected,kABPersonPhoneProperty,phoneNumberMultiValue,nil);
Please correct me if i am wrong or have misunderstood your code!

Get contacts without ABPeoplePickerNavigationController

I Want to get the contacts on the device without using ABPeoplePickerNavigationController, it is possible in objective-c?
Yes, it's possible. Below there's a simple snippet of code that do the work:
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeopleRef = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, nil, kABPersonSortByFirstName);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i = 0 ; i < nPeople ; i++ ) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeopleRef, i );
CFStringRef tmpStringRef = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSLog(#"FirstName: %#", (NSString *)tmpStringRef);
CFRelease(tmpStringRef);
}
//clean up memory
CFRelease(allPeopleRef);
CFRelease(addressBook);
For more details, please refer to the Apple Developer Documentation here.
It is. Please refer to the AddresBook programming guide. Direct interaction section.

iPhone's entire addressbook data

I want to create my custom addressbook like iphone's addressbook in which i am going to change some fields according to my needs.
So i would like to copy the iphone's entire addressbook data into my sql db.Please provide me any links or references for this.
need to know the db operation to store the addressbook data
Thanks
Instead of copying the entire address book into a DB (sync is always a pain), why not store your extended data in a DB with your DB data reference and address book entities? If you do that, you can reference an ABRecordRef id in your database.
you can load the address book entries in an NSMutable array by this procedure and then you can do whatever you want to in your application:
-(NSMutableArray*)loadContactsFromAddressBook{
NSMutableArray * contacts = [[NSMutableArray alloc] init];
ABAddressBookRef addressBook = ABAddressBookCreate( );
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex( allPeople, i );
NSString *fname = (NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
NSString *lname = (NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *name;
NSString* phoneNumber;
if (lname) {
name = [fname stringByAppendingFormat: #" %#", lname];
} else {
name = fname;
}
ABMultiValueRef phoneNumbers = ABRecordCopyValue(ref, kABPersonPhoneProperty);
int count = ABMultiValueGetCount(phoneNumbers);
if (count > 0 && name) {
phoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
[contacts addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:name, #"member_name",
phoneNumber, #"phone",
#"NO", #"invited",
nil] ] ;
}
NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"member_name" ascending:YES];
[contacts sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];
}
return [contacts autorelease];
}
you'll need to add AddressBook.framework and AddressBookUI.framework frameworks to your project, and import AddressBook/AddressBook.h file in the class you use this method.
Hope this helps.

Get addressbook group for particular contact

In my app i am getting contacts like this..
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for( int i=0;i< nPeople;i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
.....
and so on
}
can anyone suggest me how to get the group for nPeople.
Check the following documentation
http://developer.apple.com/library/mac/#documentation/UserExperience/Reference/AddressBook/Classes/ABPerson_Class/Reference/Reference.html#//apple_ref/occ/instm/ABPerson/parentGroups
There is a method parentGroups which returns the array of groups to which the person belongs...........

Why I'm getting “EXC_BAD_ACCESS” upon trying to release?

Would you please have a look at me code:
(void)loadContactsFromAddressBook {
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex count = ABAddressBookGetPersonCount(addressBook);
ABRecordRef person;
for (int i = 0; i < count; i++) {
person = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef cfStr = ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString* firstName = (NSString*) cfStr;
[contacts addObject:firstName];
CFRelease(cfStr);
}
CFRelease(addressBook);
CFRelease(allPeople);
}
I tried to CFRelease(person) after the for block but the application crashes with “EXC_BAD_ACCESS”. If I remove CFRelease(person), the application works.
You didn't create the person, you only referenced it. Insert child support joke here.