使用NSString更新kABPersonPhoneProperty来添加一个电话号码



我有一个Contact对象(我编写的一个类),我想用它来创建一个新的AddressBook人员。下面是我的代码:

+ (ABRecordRef)createABPersonFromContact:(Contact*)contact
{
    ABRecordRef person = ABPersonCreate();
    ABRecordSetValue(person, kABPersonFirstNameProperty, contact.firstName, NULL);
    ABRecordSetValue(person, kABPersonLastNameProperty, contact.lastName, NULL);
    ABRecordSetValue(person, kABPersonOrganizationProperty, contact.company, NULL);
    CFStringRef phoneNumberValue = (CFStringRef)contact.phoneNumber.value;
    CFStringRef phoneNumberLabel = (CFStringRef)contact.phoneNumber.label;
    ABMutableMultiValueRef phoneNumber = ABMultiValueCreateMutable(kABPersonPhoneProperty);
    ABMultiValueAddValueAndLabel(phoneNumber, value, label, NULL);
    ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumber, NULL);
    CFRelease(phoneNumber);
    return person;
}

contact.phoneNumber。value为NSString,格式为"555-555-5555"。当我运行代码并显示带有此方法返回的ABRecordRef的ABPersonViewController时,一切都显示正确,但如果我尝试编辑电话号码,程序就会崩溃。我尝试将NSString contact.phoneNumber.value重新格式化为"(555)555-5555"以匹配电话号码在ABPersonViewController中显示的样式,但它给了我相同的结果。

关于为什么会崩溃有什么建议吗?

问题是,我应该使用kABMultiStringPropertyType而不是kABPersonPhoneProperty为phoneNumber

相关内容

最新更新