How we show the International Phone Numbers format like as Contacts number format?

691 Views Asked by At

I like to show the international phone number on app screen like the format are used by Apple Contacts app(iPhone 6). For Example: +1 (xxx) xxx-xxxx, +91 xxxxx xxxxx, +61 (0) x xxxx xxxx, xxxxx xxxxx.

Can we deploy the standard Apple display of phone numbers format like how they arrange phone numbers in the iOS Contacts app.

Please give me some suggestion to proceed....

Is there any third party API is available to identify the number and return the number format like contact app number format. Please let me know...

I am waiting for your valuable response....

Thanks in Advance.

1

There are 1 best solutions below

1
On

Please try this code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    int groupingSize = 2;
    if([string length] == 0) {
        groupingSize = 4;
    }
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init] ;
    NSString *separator = @"-";
    [formatter setGroupingSeparator:separator];
    [formatter setGroupingSize:groupingSize];
    [formatter setUsesGroupingSeparator:YES];
    [formatter setSecondaryGroupingSize:2];
    if (![string  isEqual: @""] && (textField.text != nil && textField.text.length > 0)) {
        NSString *num = textField.text;
        num = [num stringByReplacingOccurrencesOfString:separator withString:@""];
        NSString *str = [formatter stringFromNumber:[NSNumber numberWithDouble:[num doubleValue]]];
        textField.text = str;
    }
    return YES;
}

Hope this helps!