我尝试使用以下代码来设置要添加到通讯簿的新联系人的 AIM 和 Skype 属性:
ABRecordRef record = ABPersonCreate();
CFErrorRef an_error = NULL;
ABMutableMultiValueRef multi_aim = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multi_aim, @"ExampleNick", kABPersonInstantMessageServiceAIM, NULL);
ABRecordSetValue(record, kABPersonInstantMessageProperty, multi_aim, &an_error);
ABMutableMultiValueRef multi_skype = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multi_skype, @"example.nick", kABPersonInstantMessageServiceSkype, NULL);
ABRecordSetValue(record, kABPersonInstantMessageProperty, multi_skype, &an_error);
if (an_error != NULL) {
NSLog(@"Error while creating person");
}
CFErrorRef error = NULL;
ABAddressBookRef address_book = ABAddressBookCreateWithOptions(NULL, &error);
ABAddressBookRequestAccessWithCompletion(address_book, ^(bool granted, CFErrorRef error) {
if (!granted) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Unable to access address book"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
[alert show];
} else {
BOOL is_added = ABAddressBookAddRecord(address_book, record, &error);
if (is_added) {
NSLog(@"Address book entry added");
} else {
NSLog(@"ERROR adding address book entry: %@", error);
}
error = NULL;
BOOL is_saved = ABAddressBookSave(address_book, &error);
if (is_saved) {
NSLog(@"Saved address book");
} else {
NSLog(@"ERROR saving address book: %@", error);
}
}
CFRelease(record);
CFRelease(multi_aim);
CFRelease(multi_skype);
CFRelease(address_book);
});
(我还添加了名字和姓氏,但为了简洁起见,将其排除在示例之外)
当记录被添加时,除了 AIM 和 Skype 字段之外的所有内容都会被填充。我究竟做错了什么?