Vjardel
Vjardel

Reputation: 1075

How to change the format of phone number in ABAddressBook?

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

Upvotes: 2

Views: 191

Answers (1)

Sash Zats
Sash Zats

Reputation: 5466

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

Upvotes: 2

Related Questions