How to add new contact to iOS Contacts (Address Book)?

前端 未结 1 916
深忆病人
深忆病人 2020-12-14 21:57

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

How can I do it?

相关标签:
1条回答
  • 2020-12-14 22:06

    Here is a small example :

    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 讨论(0)
提交回复
热议问题