Reputation: 61
i'm trying to make calls from my app, but it seems i can't because the numbers are in india format (example : ٩٦٦٥٩٥٨٤٨٨٨٢) and to make it work , I have to convert this string to arabic format (example : 966595848882)
my code :
NSString *cleanedString = [[ContactInfo componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *phoneNumber = [@"telprompt://" stringByAppendingString:cleanedString];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Upvotes: 2
Views: 2204
Reputation: 70956
Use NSNumberFormatter
with the appropriate locale. For example:
NSString *indianNumberString = @"٩٦٦٥٩٥٨٤٨٨٨٢";
NSNumberFormatter *nf1 = [[NSNumberFormatter alloc] init];
NSLocale *locale = [NSLocale localeWithLocaleIdentifier:@"hi_IN"];
[nf1 setLocale:locale];
NSNumber *newNum = [nf1 numberFromString:indianNumberString];
NSLog(@"new: %@", newNum);
This prints "966595848882".
I'm not 100% certain on the locale identifier above-- hi_IN
should be "Hindi India". If that's not correct, use [NSLocale availableLocaleIdentifiers]
to get a list of all known locale identifiers, and find one that's more appropriate.
Update: in order to pad this out to nine digits (or however many you want), convert back to an NSString
using standard NSString
formatting:
NSString *paddedString = [NSString stringWithFormat:@"%09ld", [newNum integerValue]];
The format %09ld
will zero-pad out to nine digits.
It's also possible to do this using the same number formatter, converting the number above back into a string while requiring 9 digits. This also gives at least nine digits, with zero padding if necessary:
[nf1 setMinimumIntegerDigits:9];
NSString *reverseConvert = [nf1 stringFromNumber:newNum];
Upvotes: 6