如何从通讯录ios以编程方式编辑电话号码值 [英] how to edit a phone number values programmatically from address book ios

查看:141
本文介绍了如何从通讯录ios以编程方式编辑电话号码值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式在iOS中使用联系人表格通讯录替换特定联系人的特定电话号码。

I'm trying to replace an specific phone number for an specific contact programmatically in iOS, taking the contacts form address book.

我不知道为什么无法保存新电话号码并刷新通讯簿以显示更改。

I don't know why I can't save the new phone number and refresh the address book to show the change.

我正在这样做:

+(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* mobilePhoneNumber=@"";


    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;
}


推荐答案

您应该调试代码并尝试找出您提供给该方法的电话号码格式是否匹配。

You should debug your code and try to figure out whether the format of the phone numbers you are providing to the method are matching or not.

例如当我记录我的联系人列表电话号码时,这些就是结果

For e.g. when i am logging my contact list phone numbers these are results

Number...555-478-7672
Number...(408) 439-5270
Number...(408) 555-3514
Number...888-555-5512
Number...888-555-1212
Number...555-522-8243
Number...(555) 766-4823
Number...(707) 555-1854
Number...555-610-6679

我正在将这些数字与未格式化的数字字符串进行比较。

And i was comparing these number against unformatted number string.

第二次

ABRecordSetValue(ABAddressBookGetPersonWithRecordID(addressBook, contactSelected),
                 kABPersonPhoneProperty,
                 phoneNumberMultiValue,
                 nil);

实际声明为

ABRecordSetValue(ABRecordRef record, ABPropertyID property, CFTypeRef value, CFErrorRef* error); 

尽管 ABAddressBookGetPersonWithRecordID 返回 ABRecordRef ,但是您已经选择了 ABRecordRef contactSelect; ,所以在我看来,您应该使用

Although ABAddressBookGetPersonWithRecordID returns a ABRecordRef but you already have ABRecordRef contactSelected; so in my view you should use

ABRecordSetValue(contactSelected,kABPersonPhoneProperty,phoneNumberMultiValue,nil);

如果我错了或误解了您的代码,请纠正我!

Please correct me if i am wrong or have misunderstood your code!

这篇关于如何从通讯录ios以编程方式编辑电话号码值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆