如何将新联系人添加到iOS联系人(通讯簿)? [英] How to add new contact to iOS Contacts (Address Book)?

查看:79
本文介绍了如何将新联系人添加到iOS联系人(通讯簿)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式将联系人直接保存到iOS设备的通讯簿中.

I want to save contact directly to an iOS device's Address Book programmatically.

我该怎么办?

推荐答案

这是一个小例子:

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);        
}

这篇关于如何将新联系人添加到iOS联系人(通讯簿)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆