2

I'm making a social App, and I have to implement ABAddressBook with name, and phone number. I would like to format phone number for example : +33 6 01 23 45 67 becomes 0601234567 (French phone number). So, I want to delete spaces, and transform +33 to 0

Some phone number aren't with spaces, but some other are. I don't know why. and number beginning with +33 6 aren't saved.

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);
Bannings
  • 10,376
  • 7
  • 44
  • 54
Vjardel
  • 1,065
  • 1
  • 13
  • 28

1 Answers1

2

Take a look at Google's solution https://github.com/googlei18n/libphonenumber.

Oddly enough, Cocoa doesn't have a public phone formatter as far as I know

Sash Zats
  • 5,376
  • 2
  • 28
  • 42