以编程方式编辑地址簿名和姓

时间:2015-06-22 09:00:23

标签: ios objective-c ios7 abaddressbook

如何以编程方式编辑特定地址簿的名字和姓氏。可以任何人指导我找到这个。

我正在接受如下的联系方式。

Email =     (
    "John-Appleseed@mac.com"
);
First = John;
  Last = Appleseed;
numbers =     (
    "888-555-5512",
    "888-555-1212"
);

}

如何编辑第一个和最后一个值。

我使用以下代码从设备

获取联系人详细信息
  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 个答案:

答案 0 :(得分:0)

首先选择所需的联系人:

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

}

参考this:设置并获取名字和姓氏

  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);