【问题标题】:How to add new contact to iOS Contacts (Address Book)?如何将新联系人添加到 iOS 联系人(通讯簿)?
【发布时间】:2011-03-22 10:01:41
【问题描述】:

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

我该怎么做?

【问题讨论】:

    标签: iphone abaddressbook


    【解决方案1】:

    这是一个小例子:

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

    【讨论】:

    • 但是我有一个小问题,保存电话号码怎么样,我们需要使用的属性是什么
    • @EshwarChaitanya 这里是完整的参考heig.ch/sohi你要搜索kABPersonPhoneProperty
    • @RanjitChandel 您在创建联系人之前是否征得了许可?这是一个请求许可的示例:gist.github.com/jfreyre/605a5e500eaf8a1b6610 注意:我没有使用ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();,自从我第一次回答以来就已被弃用,但ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL); 这是新方法
    • @j_freyre 谢谢,我错过了权限部分,现在工作正常
    • 我有联系人大型联系人列表行 1000 到 2000 联系人,我必须将其保存在联系人列表中。我查看并开始添加联系人列表。但这需要很多时间。如何克服这个问题。
    猜你喜欢
    • 1970-01-01
    • 2013-03-15
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多