
Follow the next link.
Via: Applesfera.
// | |
// 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++) | |
{ | |
ABRecordRef person = ABAddressBookGetPersonWithRecordID(ab,(ABRecordID) i); | |
CFStringRef firstName, lastName; | |
char *lastNameString, *firstNameString; | |
firstName = ABRecordCopyValue(person, kABPersonFirstNameProperty); | |
lastName = ABRecordCopyValue(person, kABPersonLastNameProperty); | |
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); | |
} | |
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 |