3
我試圖在iOS中以編程方式替換特定聯繫人的特定電話號碼,並取得聯繫人表單地址簿。如何以編程方式從通訊簿中編輯電話號碼值ios
我不知道爲什麼我無法保存新的電話號碼並刷新地址簿以顯示更改。
我這樣做:
+(BOOL) changeContactPhoneNumber:(NSString *) phoneSought
forThis:(NSString *) newPhoneNumber{
ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef contactSelected;
CFStringRef mobileLabelNumber;
CFErrorRef error = nil;
// Do whatever you want here.
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i = 0; i < nPeople; i++)
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);
NSString* [email protected]"";
if (ABMultiValueGetCount(phones) > 0) {
for (int i=0; i < ABMultiValueGetCount(phones); i++) {
[mobilePhoneNumber release];
mobilePhoneNumber = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
if([mobilePhoneNumber isEqualToString:phoneSought]){
contactSelected = ref;
mobileLabelNumber = ABMultiValueCopyLabelAtIndex(phones, i);
}
}
}
}
ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABPersonPhoneProperty);
bool didAddPhone = ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFTypeRef)newPhoneNumber,mobileLabelNumber, NULL);
if(didAddPhone){
ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected),
kABPersonPhoneProperty,
phoneNumberMultiValue,
nil);
bool bSuccess = ABAddressBookSave(addressBook, &error);
if (!bSuccess) {
NSLog(@"Could not save to address book: %@", error);
} else {
return YES;
}
} else {
NSLog(@"Error editing phone number: %@", error);
error = nil;
}
return NO;
}
我可以知道你使用的是哪個IOS版本嗎? – 2013-04-28 03:20:56
現在我正在使用從iOs 4.3到最後一個版本(6.1.3)...我有另一種方法之前,我要求的權限,如果用戶有iOs 6 – bubudrc 2013-04-28 03:33:29
你沒有提到實際發生了什麼碼。它有多遠?怎麼了? – rmaddy 2013-04-28 05:30:16