Friday, March 14, 2008

How to: Get contacts with the iPhone SDK



This source code was developed by Pablo Abad and shows the contacts stored in the iPhone Address Book. The contacts will show in the Xcode console.

Thanks Pablo, and good luck in Munich.

//
// UntitledAppDelegate.m
// Untitled
//
// Created by Pablo Abad Sanz on 12/03/08.
// Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "UntitledAppDelegate.h"
#import "MyView.h"
#import "AddressBook/AddressBook.h"

@implementation UntitledAppDelegate

@synthesize window;
@synthesize contentView;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create window
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Set up content view
self.contentView = [[[MyView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
[window addSubview:contentView];

// AddressBook
ABAddressBookRef ab;
ab = ABAddressBookCreate();
int len = (int) ABAddressBookGetPersonCount(ab);
int i;
for(i = 1; i < (len + 1); i++)
{
// Cojo el contacto 1, no sé porqué no coje el 0...
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,(ABRecordID) i);
// Ahora que lo tengo, saco su nombre y apellido
CFStringRef firstName, lastName;
char *lastNameString, *firstNameString;
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty);
lastName = ABRecordCopyValue(person, kABPersonLastNameProperty);

// Paso a char* los datos para que se puedan escribir
static char* fallback = "";
int fbLength = strlen(fallback);

int firstNameLength = fbLength;
bool firstNameFallback = true;
int lastNameLength = fbLength;
bool lastNameFallback = true;

if (firstName != NULL)
{
firstNameLength = (int) CFStringGetLength(firstName);
firstNameFallback = false;
}
if (lastName != NULL)
{
lastNameLength = (int) CFStringGetLength(lastName);
lastNameFallback = false;
}

if (firstNameLength == 0)
{
firstNameLength = fbLength;
firstNameFallback = true;
}
if (lastNameLength == 0)
{
lastNameLength = fbLength;
lastNameFallback = true;
}

firstNameString = malloc(sizeof(char)*(firstNameLength+1));
lastNameString = malloc(sizeof(char)*(lastNameLength+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);
}

// Muestro por la consola los datos
printf("%d.\t%s %s\n", i, firstNameString, lastNameString);


if (firstName != NULL)
{
CFRelease(firstName);
}
if (lastName != NULL)
{
CFRelease(lastName);
}
free(firstNameString);
free(lastNameString);
}

// Show window
[window makeKeyAndVisible];
}

- (void)dealloc {
[contentView release];
[window release];
[super dealloc];
}

@end

4 Comments:

ankur said...

Hi,
I am able to read the contacts in iPhone simulator contacts using you code.But when i add new contact in iPhone simulator then this code not give new contact which i add..
Please tell me the what's problem...
Thank you..

Sourav said...

Add the line after

ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,(ABRecordID) i);


if(parson ==null){
len++;
continue;
}

its Working Fine....

Pedro Fraca said...

Thanks for your suggestion.

Frédéric Jacobs said...

Does anyone knows how to export iPhone's AdressBook into a SQLite Database ?
If anyone has a sample code...

Thanks