如何以编程方式将"自定义标签"添加到iOS AddressBook?

Pau*_*l V 6 iphone label addressbook

在iOS地址簿中手动添加联系人的电话/ IMS时,您可以添加自定义标签而不是"主页","工作","其他"*(在IMS中).

如何以编程方式在AddressBook中创建"自定义标签"?

Mik*_*ike 12

我有同样的问题.我找不到答案所以我只是尝试了猜测和检查方法.以下代码似乎有效:

CFErrorRef error = NULL; 
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"Jane", &error);
ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Smith", &error);

const CFStringRef customLabel = CFSTR( "mylabel" );

//phone
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, @"1-444-444-444", kABPersonPhoneMainLabel, NULL);
ABMultiValueAddValueAndLabel(multiPhone, @"1-333-333-333", kABPersonPhoneMobileLabel, NULL);            
ABMultiValueAddValueAndLabel(multiPhone, @"1-666-666-666", kABOtherLabel, NULL);        
ABMultiValueAddValueAndLabel(multiPhone, @"1-555-555-555", customLabel, NULL); 
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
CFRelease(multiPhone);

ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);

if (error != NULL)
{   
    NSLog(@"Error!");   
}
Run Code Online (Sandbox Code Playgroud)

如果您查看地址簿,您将看到一个带有自定义标签的电话号码:mylabel

谢谢:这篇文章

并且:这个博客