how edit contact of Addressbook with API?

Discussion in 'iOS Programming' started by jinlianhai, Jan 5, 2009.

  1. macrumors newbie

    Joined:
    Jan 5, 2009
    #1
    Code:
    ABAddressBookRef book = ABAddressBookCreate(); 
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(book);
    ABRecordRef ref = CFArrayGetValueAtIndex(people, 1); 
    ABMultiValueRef mutablePhones =(NSString *)ABRecordCopyValue(ref,kABPersonPhoneProperty);
    ABRecordRef current = ABPersonCreate();
    NSString *phoneNumber, *phoneNumberLabel; 
    
    for (CFIndex i = 0; i < ABMultiValueGetCount(mutablePhones); i++) 
    { 
    
    phoneNumberLabel = (NSString*)ABMultiValueCopyLabelAtIndex(mutablePho nes, i); 
    
    // phone number of this record! --old---
    phoneNumber = ABMultiValueCopyValueAtIndex(mutablePhones, i);
    
    
    if ( YES == [ phoneNumberLabel isEqualToString: @"_$!<Mobile>!$_"] )
    {	
    
    NSString *mainPhone = @"111111111111111111111"; 
    
    if(ABMultiValueReplaceValueAtIndex (mutablePhones, mainPhone,i)) 
    {
    ABRecordSetValue(current, kABPersonPhoneProperty, mutablePhones, NULL);
    
    // phone number of this record! --new---
    NSString *phoneNumber2 = ABMultiValueCopyValueAtIndex(mutablePhones, i);
    
    NSLog(@"that phone number has been updated to %@.", mainPhone);
    }	
    }
    }
    
    ABAddressBookSave(book, nil);

    contact's mobile phone number was not be changed! why?
     
  2. macrumors newbie

    Joined:
    Jun 11, 2008
    #2
    ABMultiValueCreateMutableCopy

    Hi,

    you need to do a mutableCopy before.

    ABAddressBookRef book = ABAddressBookCreate();
    CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(book);
    ABRecordRef ref = CFArrayGetValueAtIndex(people, 1);
    ABMultiValueRef mutablePhones =(NSString *)ABMultiValueCreateMutableCopy(ABRecordCopyValue(ref,kABPersonPhoneProperty));
    ABRecordRef current = ABPersonCreate();
    NSString *phoneNumber, *phoneNumberLabel;
     

Share This Page