如何在ABAddressBook中更改电话号码的格式?

时间:2015-08-29 23:47:59

标签: ios objective-c format abaddressbook nsmutablestring

我正在制作社交应用,我必须使用名称和电话号码来实现ABAddressBook。 我想格式化电话号码,例如:+33 6 01 23 45 67变为0601234567(法语电话号码)。因此,我想删除空格,并将+33转换为0

某些电话号码没有空格,但其他一些电话号码。我不知道为什么。以+33 6开头的号码不会被保存。

ABMultiValueRef phones = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
for (NSUInteger j = 0; j < ABMultiValueGetCount(phones); j++) {

    NSMutableString *phone = [CFBridgingRelease(ABMultiValueCopyValueAtIndex(phones, j)) mutableCopy];

    [phone replaceOccurrencesOfString:@" "    withString:@""   options:0 range:NSMakeRange(0, phone.length)];
    [phone replaceOccurrencesOfString:@"("    withString:@""   options:0 range:NSMakeRange(0, phone.length)];
    [phone replaceOccurrencesOfString:@")"    withString:@""   options:0 range:NSMakeRange(0, phone.length)];
    [phone replaceOccurrencesOfString:@"-"    withString:@""   options:0 range:NSMakeRange(0, phone.length)];
    [phone replaceOccurrencesOfString:@"+33" withString:@"0" options:0 range:NSMakeRange(0, phone.length)];
    [phone stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

    if (((j == 0) && [phone hasPrefix:@"06"]) || ((j == 0) && [phone hasPrefix:@"07"]))     person.mainNumber   = phone;
    else if ((j==1) && [phone hasPrefix:@"06"])  person.mainNumber = phone;
    else if ((j==2) && [phone hasPrefix:@"06"])  person.mainNumber = phone;
    else if ((j==3) && [phone hasPrefix:@"06"])  person.mainNumber = phone;
}
CFRelease(phones);

1 个答案:

答案 0 :(得分:2)

查看Google的解决方案https://github.com/googlei18n/libphonenumber

奇怪的是,就我所知,Cocoa没有公用电话格式化器