【问题标题】:How to change the format of phone number in ABAddressBook?如何更改ABAddressBook中电话号码的格式?
【发布时间】:2015-08-29 23:47:58
【问题描述】:

我正在制作一个社交应用程序,我必须使用姓名和电话号码来实现 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);

【问题讨论】:

    标签: ios objective-c format abaddressbook nsmutablestring


    【解决方案1】:

    看看谷歌的解决方案https://github.com/googlei18n/libphonenumber

    奇怪的是,据我所知 Cocoa 没有公用电话格式化程序

    【讨论】:

      猜你喜欢
      • 2013-07-14
      • 1970-01-01
      • 2015-08-09
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      相关资源
      最近更新 更多