Friday, April 29, 2011

Get the Property form address contact in iPhone




strFirstName = (NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

//Get mobile phone number
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
mobile=@"";
NSString* mobileLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
NSLog(@"",mobileLabel);
if([mobileLabel isEqualToString:@"_$!!$_"]) {
mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
}
}
//get the email
//Get mobile phone number
ABMultiValueRef emailAddress =(NSString*)ABRecordCopyValue(person, kABPersonEmailProperty);
email=@"";
NSString* emailLabel;
for(CFIndex i = 0; i < ABMultiValueGetCount(emailAddress); i++) {
emailLabel = (NSString*)ABMultiValueCopyLabelAtIndex(emailAddress, i);
NSLog(@"",emailLabel);

if([emailLabel isEqualToString:@"_$!!$_"]) {
email = (NSString*)ABMultiValueCopyValueAtIndex(emailAddress, i);
}
}


- (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; 
}