Reputation: 4055
I want to be able to import contact group data when I import contacts in my application. Is there a method for me to get this data from iOS or am I just limited to get contact details?
Upvotes: 1
Views: 1375
Reputation: 908
- (void)fetchMobileContacts
{
CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
});
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}
else { // we're on iOS 5 or older
accessGranted = YES;
}
if (accessGranted) {
// Do whatever you want here.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople( addressBook );
CFIndex nPeople = ABAddressBookGetPersonCount( addressBook );
NSMutableArray *validAddress = [NSMutableArray new];
for ( int i = 0; i < nPeople; i++ )
{
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
ABMultiValueRef phoneNumberProperty = ABRecordCopyValue(person, kABPersonPhoneProperty);
NSArray* phoneNumbers = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumberProperty);
ABMultiValueRef emailProperty = ABRecordCopyValue(person, kABPersonEmailProperty);
NSArray* emails = (__bridge NSArray*)ABMultiValueCopyArrayOfAllValues(emailProperty);
NSString *email = @"";
NSString *phone = @"";
if(phoneNumbers.count) phone = [phoneNumbers objectAtIndex:0];
if(emails.count) email = [phoneNumbers objectAtIndex:0];
if(email.length || phone.length){
NSDictionary *contactInfo = @{kContactName: fullName, kContactPhone: phone, kContactEmail: fullName};
[validAddress addObject:contactInfo];
}
}
MobileContactsViewController *contactsVC = [[MobileContactsViewController alloc] initWithContacts:validAddress];
contactsVC.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:contactsVC animated:YES];
}
}
Upvotes: 0
Reputation: 50707
This is how I import contact information into my app. I store the info in an NSDictionary
so I can read it back later easily. It's in no way pretty, but works very well.
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface YourViewController : UIViewController <ABPeoplePickerNavigationControllerDelegate>
{
}
@end
#pragma mark -
#pragma mark AddressBook Delegates
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
return NO;
}
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
[self dismissModalViewControllerAnimated:YES];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
/* photo */
CFDataRef imgeData = ABPersonCopyImageData(person);
if(imgeData)
{
/* set an image to something using: (NSData*)imgeData */
CFRelease(imgeData);
}else{
/* theres no photo */
}
CFStringRef email,emailLabel, phone,phoneLabel, url,urlLabel;
//Phone Numbers
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];
//NSLog(@"%@: %@",phoneLabel,phone);
CFRelease(phone);
CFRelease(phoneLabel);
}
if ( [myPhoneDict objectForKey:@"mobile"] != nil) {
[plistDict setObject:[NSString stringWithFormat:@"%@",[myPhoneDict objectForKey:@"mobile"]] forKey:@"Phone"];
} else if ( [myPhoneDict objectForKey:@"home"] != nil) {
[plistDict setObject:[NSString stringWithFormat:@"%@",[myPhoneDict objectForKey:@"home"]] forKey:@"Phone"];
} else if ( [myPhoneDict objectForKey:@"work"] != nil) {
[plistDict setObject:[NSString stringWithFormat:@"%@",[myPhoneDict objectForKey:@"work"]] forKey:@"Phone"];
}
//Email Address
ABMutableMultiValueRef emailMulti = ABRecordCopyValue(person, kABPersonEmailProperty);
NSMutableDictionary *myEmailDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(emailMulti)];
for (CFIndex i = 0; i < ABMultiValueGetCount(emailMulti); i++) {
emailLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(emailMulti, i));
email = ABMultiValueCopyValueAtIndex(emailMulti, i);
[myEmailDict setObject:(NSString*)email forKey:(NSString*)emailLabel];
CFRelease(email);
CFRelease(emailLabel);
}
if ([myEmailDict objectForKey:@"home"] != nil) {
[plistDict setObject:[myEmailDict objectForKey:@"home"] forKey:@"EmailHome"];
} else {
[plistDict setObject:@"" forKey:@"Email"];
} if ([myEmailDict objectForKey:@"work"] != nil) {
[plistDict setObject:[myEmailDict objectForKey:@"work"] forKey:@"EmailWork"];
} else {
[plistDict setObject:@"" forKey:@"Email"];
} if ([myEmailDict objectForKey:@"other"] != nil) {
[plistDict setObject:[myEmailDict objectForKey:@"other"] forKey:@"EmailOther"];
} else {
[plistDict setObject:@"" forKey:@"Email"];
}
//Website Address
ABMutableMultiValueRef URLMulti = ABRecordCopyValue(person, kABPersonURLProperty);
NSMutableDictionary *myURLDict = [NSMutableDictionary dictionaryWithCapacity:ABMultiValueGetCount(URLMulti)];
for (CFIndex i = 0; i < ABMultiValueGetCount(URLMulti); i++) {
urlLabel = ABAddressBookCopyLocalizedLabel(ABMultiValueCopyLabelAtIndex(URLMulti, i));
url = ABMultiValueCopyValueAtIndex(URLMulti, i);
[myURLDict setObject:(NSString*)url forKey:(NSString*)urlLabel];
//NSLog(@"%@",myURLDict);
CFRelease(url);
CFRelease(urlLabel);
}
if ([myURLDict objectForKey:@"home page"] != nil) {
[plistDict setObject:[myURLDict objectForKey:@"home page"] forKey:@"Website"];
}else{
[plistDict setObject:@"" forKey:@"Website"];
}
//Full Address
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)){
[plistDict setObject:street forKey:@"Street"];
}else{
[plistDict setObject:@"" forKey:@"Street"];
}
if ((city != nil) && (city.length > 0)){
[plistDict setObject:city forKey:@"City"];
}else{
[plistDict setObject:@"" forKey:@"City"];
}
if ((state != nil) && (state.length > 0)){
[plistDict setObject:state forKey:@"State"];
}else{
[plistDict setObject:@"" forKey:@"State"];
}
if ((zip != nil) && (zip.length > 0)){
[plistDict setObject:zip forKey:@"Zip"];
}else{
[plistDict setObject:@"" forKey:@"Zip"];
}
if ((country != nil) && (country.length > 0)){
[plistDict setObject:country forKey:@"Country"];
}else{
[plistDict setObject:@"" forKey:@"Country"];
}
if (type != nil){
[plistDict setObject:(NSString *)type forKey:@"AddressType"];
}else{
[plistDict setObject:@"" forKey:@"AddressType"];
}
[myLabelDict release];
[street release];
[city release];
[state release];
[zip release];
[country release];
CFRelease(dict);
CFRelease(type);
}
CFRelease(streets);
CFRelease(phoneMulti);
CFRelease(emailMulti);
CFRelease(URLMulti);
[plistDict writeToFile:current atomically:YES];
[self dismissModalViewControllerAnimated:YES];
[self refreshViews];
[tblAddCarrier reloadData];
return YES;
}
Upvotes: 2