我正在尝试从我的应用向通讯录添加联系人。我拥有的代码效果很好,但我希望联系人姓名为NSString的值。我试图用字符串名称替换代码中的@“ David”,但它似乎不起作用。

CFErrorRef error = nil;
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
ABRecordRef newPerson = ABPersonCreate();
ABRecordSetValue(newPerson, kABPersonFirstNameProperty,@"David", nil);
ABRecordSetValue(newPerson, kABPersonLastNameProperty,@"Olsen", nil);

//Add my phone number

ABMutableMultiValueRef phoneNumber = ABMultiValueCreateMutable(kABStringPropertyType);
ABMultiValueAddValueAndLabel(phoneNumber,
                             @"095", kABPersonPhoneMainLabel, nil);
ABRecordSetValue(newPerson, kABPersonEmailProperty, phoneNumber, nil);


//Finally saving the contact in the address book
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL)
{
    NSLog(@"Saving contact failed.");
}


NSLog(@"Contact saved.");


}

最佳答案

您必须使用CFStringRef,所以__bridge您的NSString

NSString *name = @David";
ABRecordSetValue(newPerson, kABPersonFirstNameProperty,(__bridge  CFStringRef) name, nil);

关于ios - iOS通讯录:来自NSString值的联系人姓名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22333971/

10-12 15:42