Edit the Address book First and Last Name Programatically

235 views Asked by At

How can i Edit Specific Address book First Name and Last Name Programatically .Could any one Guide me to found this.

Am getting Contact Recode like below.

Email =     (
    "[email protected]"
);
First = John;
  Last = Appleseed;
numbers =     (
    "888-555-5512",
    "888-555-1212"
);

}

How can i edit the First and last Values.

I used Below code to Fetch contact details from Device

  CFErrorRef * error = NULL;
addressBook = ABAddressBookCreateWithOptions(NULL, error);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
                                         {
                                             if (granted)
                                             {
                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
                                                     CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

                                                     for(int i = 0; i < numberOfPeople; i++){
                                                         ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

                                                         NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
                                                         NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
                                                         NSString *name=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                                                         NSString *Birthday=(__bridge NSString *)(ABRecordCopyValue(person, kABPersonBirthdayProperty));
                                                         NSLog(@"Name:%@ %@ %@", firstName, lastName,Birthday);

                                                         //For Email ids
                                                         NSMutableArray *Emails = [NSMutableArray array];

                                                         ABMutableMultiValueRef eMail  = (__bridge ABMutableMultiValueRef)((__bridge NSString *) ABRecordCopyValue(person, kABPersonEmailProperty));

                                                         for (CFIndex i = 0; i < ABMultiValueGetCount(eMail); i++) {

                                                             NSString *emails = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(eMail, i);

                                                             [Emails addObject:emails];


                                                         }


                                                         ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

                                                         NSMutableArray *numbers = [NSMutableArray array];
                                                         for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
                                                             NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);
                                                             [numbers addObject:phoneNumber];
                                                         }


                                                         NSMutableDictionary *contact = [NSMutableDictionary dictionary];
                                                         [contact setObject:firstName forKey:@"First"];
                                                         [contact setObject:lastName forKey:@"Last"];

                                                         [contact setObject:numbers forKey:@"numbers"];
                                                         [contact setObject:Emails forKey:@"Email"];

                                                         NSLog(@" contacts are %@",contact);


                                                         [all_contacts addObject:contact];


                                                         NSLog(@"all contacts are %@",all_contacts);

                                                         [self.tbl_contacts reloadData];





                                                     }
                                                 });
                                             }
                                         });
1

There are 1 answers

1
soumya On

First pick the required contact:

    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePicker
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
     {
      [self peoplePickerNavigationController:peoplePicker didSelectPerson:person];
      return NO;

}

Refer this: for setting and getting first name and last name

  ABRecordRef aRecord = ABPersonCreate();
  CFErrorRef anError = NULL;
  bool didSet;

  didSet = ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Katie"), &anError);
   if (!didSet) {/* Handle error here. */}

    didSet = ABRecordSetValue(aRecord, kABPersonLastNameProperty, CFSTR("Bell"), &anError);
    if (!didSet) {/* Handle error here. */}

   CFStringRef firstName, lastName;
   firstName = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty);
   lastName  = ABRecordCopyValue(aRecord, kABPersonLastNameProperty);

    /* ... Do something with firstName and lastName. ... */

   CFRelease(aRecord);
   CFRelease(firstName);
   CFRelease(lastName);