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

标签 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);

最佳答案

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

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

关于ios - 如何更改ABAddressBook中电话号码的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32291800/

相关文章:

javascript - 检测移动到 Mobile Safari 中的新选项卡

ios - 允许 URLRequest httpBody 中使用特殊字符

iOS 9 隐藏状态栏返回按钮

ios - 无效的浮点值 - swift 3 ios

java 1.8 : String#format eating characters when called without format params

ios - (iOS)后台运行下载任务

ios - 在IOS中,从相机中获取静态图像并直接将其放入内存中,而不保存到存储中

ios - Obj-c中数组的文字语法

iOS NSDateFormatter dateFromString 仅在一个实例中返回 nil

Javascript - 格式化数字以始终显示原始小数位