How to add a “Custom Label” to iOS AddressBook programmatically?

后端 未结 1 525
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-09 17:57

When manually adding a contact\'s phone / IMS in the iOS AddressBook, you can add a Custom Label instead of \"Home\", \"Work\", \"Other\" * (in IMS).

How to create \"Cus

1条回答
  •  鱼传尺愫
    2021-02-09 18:52

    I had this exact same question. I couldn't find an answer so I just tried the guess and check method. The following code seems to work:

    CFErrorRef error = NULL; 
    ABAddressBookRef iPhoneAddressBook = ABAddressBookCreate();
    ABRecordRef newPerson = ABPersonCreate();
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, @"Jane", &error);
    ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"Smith", &error);
    
    const CFStringRef customLabel = CFSTR( "mylabel" );
    
    //phone
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, @"1-444-444-444", kABPersonPhoneMainLabel, NULL);
    ABMultiValueAddValueAndLabel(multiPhone, @"1-333-333-333", kABPersonPhoneMobileLabel, NULL);            
    ABMultiValueAddValueAndLabel(multiPhone, @"1-666-666-666", kABOtherLabel, NULL);        
    ABMultiValueAddValueAndLabel(multiPhone, @"1-555-555-555", customLabel, NULL); 
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
    CFRelease(multiPhone);
    
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);
    ABAddressBookSave(iPhoneAddressBook, &error);
    
    if (error != NULL)
    {   
        NSLog(@"Error!");   
    }
    

    If you check the address book, you will see a phone number with a custom label: mylabel

    Thanks to: this post

    And to: this blog

    0 讨论(0)
提交回复
热议问题