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