我想在通讯录中创建一个新联系人。当您想要存储更多 URL(某些社交网络的网址)时,就会出现问题。我的代码在 iOS6 的模拟器中完美运行。但在 iOS6 的真实 iPhone 中,存储了除 urls 之外的所有值。我一直在寻找几天,但找不到解决方案,如果有人能提供帮助,我将不胜感激。
我的代码:
-(void) addContactToAddressBook:(ABAddressBookRef) iPhoneAddressBook
{
CFErrorRef error = NULL;
ABRecordRef newPerson = ABPersonCreate();
//Name and phone number
ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
//Email value
ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
CFRelease(emailMultiValue);
//URL values
ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)@"Facebook", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)@"Twitter", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)@"Linkedin", NULL);
ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)@"Google+", NULL);
ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
CFRelease(urlMultiValue);
ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
ABAddressBookSave(iPhoneAddressBook, &error);
if (error != NULL)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
[alert show];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact saved" message:@"Your contact was successfully saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
谢谢!