如何将新联系人添加到iOS通讯簿?

问题描述:

我想以编程方式将联系人直接保存到iOS设备的地址簿。如何将新联系人添加到iOS通讯簿?

我该怎么办?

这里是一个小例子:

CFErrorRef error = NULL; 
NSLog(@"%@", [self description]); 
ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate(); 

ABRecordRef newPerson = ABPersonCreate(); 

ABRecordSetValue(newPerson, kABPersonFirstNameProperty, people.firstname, &error); 
ABRecordSetValue(newPerson, kABPersonLastNameProperty, people.lastname, &error); 

    ABMutableMultiValueRef multiPhone =  ABMultiValueCreateMutable(kABMultiStringPropertyType); 
ABMultiValueAddValueAndLabel(multiPhone, people.phone, kABPersonPhoneMainLabel, NULL); 
ABMultiValueAddValueAndLabel(multiPhone, people.other, kABOtherLabel, NULL);    
ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil); 
CFRelease(multiPhone); 
    // ... 
    // Set other properties 
    // ... 
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error); 

ABAddressBookSave(iPhoneAddressBook, &error); 
    CFRelease(newPerson); 
    CFRelease(iPhoneAddressBook); 
if (error != NULL) 
{ 
     CFStringRef errorDesc = CFErrorCopyDescription(error); 
    NSLog(@"Contact not saved: %@", errorDesc); 
     CFRelease(errorDesc);   
} 
+0

但我有一个小问题,怎么样保存电话号码,什么是我们需要利用 – 2012-07-04 10:36:45

+0

@EshwarChaitanya这里是完整的参考HTTP属性: //heig.ch/sohi你必须搜索'kABPersonPhoneProperty' – 2012-07-05 05:20:23

+0

是啊,它已经Mr.J_freyre – 2012-07-05 05:22:07