如何编辑电子邮件和电话号码值以编程方式从iOS 5 +中的地址簿中获取

时间:2012-09-24 07:45:07

标签: email ios5 abaddressbook phone-number

我想发电子邮件&电话号码值以编程方式从iOS 5 +中的地址簿中获取。我试过以下代码。它改变了名字和名字。姓氏但不是电子邮件&电话号码。 这是我的代码

ABAddressBookRef addressBook=ABAddressBookCreate();
ABRecordRef record = ABAddressBookGetPersonWithRecordID(addressBook, [userInfo.userId intValue]);
NSNumber *recordId = [NSNumber numberWithInteger:ABRecordGetRecordID(record)];
NSLog(@"recordId:- %d",[recordId intValue]);

// First & Last name
ABRecordSetValue(record, kABPersonFirstNameProperty, (__bridge void*)userInfo.firstName , nil); 
ABRecordSetValue(record, kABPersonLastNameProperty, (__bridge void*)userInfo.lastName, nil);

//Phone number is a list , so create a multivalue    
ABMutableMultiValueRef phoneNumberMultiValue =
ABMultiValueCreateMutable(kABPersonPhoneProperty);
ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge void*)userInfo.phoneNumbers,(__bridge void*)userInfo.phoneNumberLabel, NULL);

// Email address is a list , so create a multivalue
ABMutableMultiValueRef emailMultiValue =
ABMultiValueCreateMutable(kABPersonEmailProperty);
ABMultiValueAddValueAndLabel(emailMultiValue ,(__bridge void*)userInfo.emailAddress,(__bridge void*)userInfo.emailAddressLabel, NULL);

// Save Address Book
BOOL isContactEdited = ABAddressBookSave(addressBook, nil);
CFRelease(addressBook);
return isContactEdited;

在上面的代码中,phonenumberlabel& emailAddressLabel是电话号码和电话号码的标签值。电子邮件地址 。例如“移动”或“家庭”等。

我找不到设置multivaluerefs的方法,比如用于名字&姓。 任何形式的帮助表示赞赏。感谢。

1 个答案:

答案 0 :(得分:1)

首先,当您从PeoplePickerNavigationController中选择联系人时:

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {    

    self.selectedPerson = ABRecordGetRecordID(person);

    (...)

}

然后在我的.h

@property (nonatomic) ABRecordID selectedPerson;

//向本地联系人添加电子邮件

然后回到.m

ABAddressBookRef addressBook = ABAddressBookCreate();
CFErrorRef error = nil;
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABPersonEmailProperty);
bool didAddEmail = ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFTypeRef)(email), kABOtherLabel, NULL);

if(didAddEmail){
    ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, self.selectedPerson),
                     kABPersonEmailProperty,
                     emailMultiValue,
                     nil);
} else {
    NSLog(@"Error adding email: %@", error);
    error = nil;
}

然后在某处您需要在代码中的某处保存地址簿......

bool bSuccess = ABAddressBookSave(addressBook, &error);
if (!bSuccess) {
    NSLog(@"Could not save to address book: %@", error);
}