Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum

View Single Post
Old 07-01-2009, 06:14 PM   #6 (permalink)
BSDimwit
Registered Member
 
Join Date: Sep 2008
Location: Denver
Posts: 172
Default Here is my code

Here is my code for plucking useful bits out of the addressBook, see if you can use any of it. This code works on both the sim and phone.

Code:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker 
	  shouldContinueAfterSelectingPerson:(ABRecordRef)person { 
	
	customer.firstName = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
	customer.lastName = (NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
	customer.companyName = (NSString *)ABRecordCopyValue(person, kABPersonOrganizationProperty);
	
	if (!customer.firstName.length) {
		customer.firstName = @"*";
	}
	
	if (!customer.lastName.length) {
		customer.lastName = @"*";
	}
	
	if (!customer.companyName.length) {
		customer.companyName = @"";
	}


	CFStringRef email, emailLabel, phone, phoneLabel;
	
	ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(person, kABPersonPhoneProperty);
	NSMutableDictionary *myPhoneDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(phoneMulti)];
	for (CFIndex i = 0; i < ABMultiValueGetCount(phoneMulti); i++) { 
		
		phoneLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(phoneMulti, i));
		phone = ABMultiValueCopyValueAtIndex(phoneMulti, i); 
		[myPhoneDict setObject:(NSString*)phone forKey:(NSString*)phoneLabel];
		CFRelease(phone);
		CFRelease(phoneLabel);
		
	} 
	
	if ( [myPhoneDict objectForKey:@"mobile"] != nil) {
		customer.phone  = [myPhoneDict objectForKey:@"mobile"];
	} else if ( [myPhoneDict objectForKey:@"home"] != nil) {
		customer.phone  = [myPhoneDict objectForKey:@"home"];
	} else if ( [myPhoneDict objectForKey:@"work"] != nil) {
		customer.phone  = [myPhoneDict objectForKey:@"work"];
	}
	

	ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
	NSMutableDictionary *myEmailDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(multi)];
	for (CFIndex i = 0; i < ABMultiValueGetCount(multi); i++) { 
		
		emailLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(multi, i));
		email = ABMultiValueCopyValueAtIndex(multi, i); 
		[myEmailDict setObject:(NSString*)email forKey:(NSString*)emailLabel];
		CFRelease(email);
		CFRelease(emailLabel);
		
	} 
	
	if ([myEmailDict objectForKey:@"home"] != nil) {
		customer.email = [myEmailDict objectForKey:@"home"];
	} else if ([myEmailDict objectForKey:@"work"] != nil) {
		customer.email = [myEmailDict objectForKey:@"work"];
	} else if ([myEmailDict objectForKey:@"other"] != nil) {
		customer.email = [myEmailDict objectForKey:@"other"];
	}
	
	// get the address stuff
	
	ABMultiValueRef streets = ABRecordCopyValue(person, kABPersonAddressProperty);
	NSMutableDictionary *myAddressDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(streets)];
	for (CFIndex j = 0; j < ABMultiValueGetCount(streets); j++) {
		NSMutableDictionary *myLabelDict = [[NSMutableDictionary alloc] init];
		CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);
		CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(streets, j);
		CFStringRef type = ABAddressBookCopyLocalizedLabel(typeTmp);
		NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
		NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
		NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
		NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
		NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];
		
		if ((street != nil) && (street.length > 0))
			[myLabelDict setObject:street forKey:@"street"];
		if ((city != nil) && (city.length > 0))	
			[myLabelDict setObject:city forKey:@"city"];
		if ((state != nil) && (state.length > 0))
			[myLabelDict setObject:state forKey:@"state"];
		if ((zip != nil) && (zip.length > 0))	
			[myLabelDict setObject:zip forKey:@"zip"];
		if ((country != nil) && (country.length > 0))	
			[myLabelDict setObject:country forKey:@"country"];
		
		[myAddressDict setObject:myLabelDict forKey:(NSString *)type];
		[myLabelDict release];
		[street release];
		[city release];
		[state release];
		[zip release];
		[country release];
		
		CFRelease(dict);
		CFRelease(type);
		CFRelease(typeTmp);
	}

	
	NSString *theKey;
	if ([myAddressDict objectForKey:@"home"] != nil) {
		theKey = @"home";
	} else if ([myAddressDict objectForKey:@"work"] != nil) {
		theKey = @"work";
	} else if ([myAddressDict objectForKey:@"other"] != nil) {
		theKey = @"other";
	}
	
	if ([[[myAddressDict objectForKey:theKey] objectForKey:@"street"] length] > 0) {
		customer.street = [[myAddressDict objectForKey:theKey] objectForKey:@"street"];
	} else {
		customer.street = @"";
	}
	
	if ([[[myAddressDict objectForKey:theKey] objectForKey:@"city"] length] > 0) {
		customer.city = [[myAddressDict objectForKey:theKey] objectForKey:@"city"];
	} else {
		customer.city = @"";
	}
	
	if ([[[myAddressDict objectForKey:theKey] objectForKey:@"state"] length] > 0) {
		customer.state = [[myAddressDict objectForKey:theKey] objectForKey:@"state"];
	} else {
		customer.state = @"";
	}
	
	if ([[[myAddressDict objectForKey:theKey] objectForKey:@"zip"] length] > 0) {
		customer.postalCode = [[myAddressDict objectForKey:theKey] objectForKey:@"zip"];
	} else {
		customer.postalCode = @"";
	}
	
	if ([[[myAddressDict objectForKey:theKey] objectForKey:@"country"] length] > 0) {
		customer.country = [[myAddressDict objectForKey:theKey] objectForKey:@"country"];
	} else {
		customer.country = @"";
	}
	
	
	CFRelease(streets);
	CFRelease(phoneMulti);
	CFRelease(multi);
	
	[customer dehydrate];
	
	[self dismissModalViewControllerAnimated:YES];
	return NO; 
	
}
BSDimwit is offline   Reply With Quote
 

» Advertisements
» Stats
Members: 158,718
Threads: 89,177
Posts: 380,483
Top Poster: BrianSlick (7,120)
Welcome to our newest member, korting
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.