0

I have this code to open the contacts view controller:

ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
               newPersonVC.newPersonViewDelegate = self;
               UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
               [self presentModalViewController:nc animated:YES];

I want to be able to grab NSString *phoneNumber = @"(416)-555-5555"; and put it into the "home phone" field. I have looked through other answers and I end up getting either nothing popping up or my app crashes. Apple's documentation doesn't help much either.

How do I solve this? Any help is appreciated.

Community
  • 1
  • 1
chrisjr
  • 760
  • 3
  • 11
  • 18

1 Answers1

2
NSString *phoneNumber = @"(416)-555-5555";
ABRecordRef newPerson = ABPersonCreate();
CFErrorRef error = NULL;
ABRecordSetValue(newPerson, kABPersonDepartmentProperty,(__bridge  CFTypeRef)@"Department", nil);
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue,(__bridge  CFTypeRef)phoneNumber, kABWorkLabel, nil);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
NSAssert( !error, @"Something bad happened here." );

ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
[newPersonVC setDisplayedPerson:newPerson];
newPersonVC.newPersonViewDelegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
[self presentModalViewController:nc animated:YES];

Hope it Help's you.

MadhuP
  • 2,019
  • 17
  • 22
  • Works like a charm! Thanks for your help; this has been very frustrating. Such a shame that it has to be this difficult for Apple to update and modernize the code. – chrisjr Nov 15 '12 at 12:56
  • Just curious though: what's the code for adding a phone number to an existing contact? – chrisjr Nov 15 '12 at 12:58
  • 1
    ABAddressBookRef addressbook = ABAddressBookCreate(); NSArray *people = (__bridge NSArray *)ABAddressBookCopyPeopleWithName(addressbook, CFSTR("ContactName")); if ((people != NULL) && [people count]) { ABRecordRef person = (__bridge ABRecordRef)[people objectAtIndex:0]; ABPersonViewController *picker = [[ABPersonViewController alloc]init]; picker.personViewDelegate = self; picker.displayedPerson = person; picker.allowsEditing = YES; [self.navigationController pushViewController:picker animated:YES]; } CFRelease(addressbook); – MadhuP Nov 15 '12 at 13:24