2011-01-19 31 views
4

我試圖通過我的應用程序更新地址簿中現有聯繫人的內容,但不需要UI。該方案是這樣的:在ABAddressBook中向現有的ABRecord添加新號碼 - iPhone

1所述的用戶輸入一個號碼和姓名 2所述的應用程序檢查該名稱是在聯繫人列表中 3如果是則它檢查該號碼是否爲所述觸點中的一個名稱 4如果不是它將它添加到該名稱

我已經設法實現步驟1-3,但我找不到方法做4.可以幫助嗎?

下面,如果我的代碼看起來像

... 
CFIndex lTotalContactsCount = ABAddressBookGetPersonCount(lAddressBook); 
NSArray *people = (NSArray *)ABAddressBookCopyArrayOfAllPeople(lAddressBook); 

for (CFIndex i = 0; i < lTotalContactsCount; i++) 
{ 
    ABRecordRef lRef = (ABRecordRef)[people objectAtIndex:i]; 

    ... 
    // if names match 
    { 
     ABMutableMultiValueRef lPhoneNumbers = ABRecordCopyValue(lRef, kABPersonPhoneProperty); 
     CFIndex lContactPhoneNumberCount = ABMultiValueGetCount(lPhoneNumbers); 
     ABRecordID contactID = ABRecordGetRecordID(lRef); 

     ... 
     // if numbers dont match 
     { 
        // THIS BIT IS NOT WOKRING 
      CFErrorRef error = NULL; 

      ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType); 
      ABMultiValueAddValueAndLabel(multiPhone, number, (CFStringRef)@"Duplicate", NULL); 

     // ABRecordSetValue(newPerson, kABPersonFirstNameProperty, name, &error); 

      //add the number to the contact 
      ABRecordSetValue(lRef, kABPersonPhoneProperty, multiPhone,nil); 
     // ABAddressBookAddRecord(lAddressBook, lRef, &error); 
      ABAddressBookSave(lAddressBook, &error); 
     } 

     if(firstName) 
      CFRelease(firstName); 
     if(lastName) 
      CFRelease(lastName); 
     if(lPhoneNumbers) 
      CFRelease(lPhoneNumbers); 

     // no need to search other entries 
     if(numberExists) 
      break; 
    } 

回答

7

後進一步在API的早上看我設法找到解決方案。你在這裏:

// contactId is the ID of the person i need to add a new number to his contacts 
// got the id through : ABRecordGetRecordID(ABRecordRef) 
ABRecordRef person = ABAddressBookGetPersonWithRecordID(lAddressBook, contactID); 
ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutableCopy(lPhoneNumbers); 
ABMultiValueAddValueAndLabel(multiPhone, number, (CFStringRef)@"Duplicate", NULL);  
//add the number to the contact 
ABRecordSetValue(person, kABPersonPhoneProperty, multiPhone,nil); 
ABAddressBookSave(lAddressBook, &error); 
相關問題