Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RMPhoneFormat/RMPhoneFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- (id)initWithDefaultCountry:(NSString *)countryCode;

- (NSString *)format:(NSString *)str;
- (NSString *)absoluteInternationalFormat:(NSString *)str;

// Calling code for the user's default country based on their Region Format setting
- (NSString *)defaultCallingCode;
Expand Down
49 changes: 49 additions & 0 deletions RMPhoneFormat/RMPhoneFormat.m
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,55 @@ - (NSString *)format:(NSString *)orig {
//return orig;
}

- (NSString *)absoluteInternationalFormat:(NSString *)orig {
// Don't deal with invalid numbers.
if (![self isPhoneNumberValid:orig]) {
return nil;
}

NSString *str = [RMPhoneFormat strip:orig];

if ([str hasPrefix:@"+"]) {
// This is already an absolute number -- return the stripped version.
return str;
}

CallingCodeInfo *info = [self callingCodeInfo:_defaultCallingCode];
if (info == nil) {
// No way to determine absolute international format for this number.
return nil;
}

NSString *absoluteInternational;

// See if the entered number begins with an access code valid for the user's region format.
NSString *accessCode = [info matchingAccessCode:str];
if (accessCode) {
// Strip off the access code -- the rest should start with a country code followed by
// the regional number.
NSString *rest = [str substringFromIndex:[accessCode length]];
absoluteInternational = [@"+" stringByAppendingString:rest];
} else {
// No international prefix.
NSString *trunkCode = [info matchingTrunkCode:str];
if (trunkCode.length) {
str = [str substringFromIndex:trunkCode.length];
}

// Attempt to create a full-fledged +xx number from this number to see if it's valid.
absoluteInternational = [NSString stringWithFormat:@"+%@%@", info.callingCode, str];
}

if (![self isPhoneNumberValid:absoluteInternational]) {
// This might not be valid eg. in the case of regional numbers, like
// 555-5555 in the US — not direct-dialable from outside the country.
return nil;
}

return absoluteInternational;
}


- (BOOL)isPhoneNumberValid:(NSString *)phoneNumber {
// First remove all added punctuation to get just raw phone number characters.
NSString *str = [RMPhoneFormat strip:phoneNumber];
Expand Down
34 changes: 32 additions & 2 deletions RMPhoneFormat/RMViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ @implementation RMViewController {
RMPhoneFormat *_phoneFormat;
NSMutableCharacterSet *_phoneChars;
UITextField *_textField;
UILabel *_absoluteNumberLabel;
}

- (id)init {
Expand Down Expand Up @@ -92,6 +93,22 @@ - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRang
BOOL valid = [_phoneFormat isPhoneNumberValid:phone];

textField.textColor = valid ? [UIColor blackColor] : [UIColor redColor];

if (valid) {
NSString *absoluteNumber = [_phoneFormat absoluteInternationalFormat:txt];

if (absoluteNumber) {
_absoluteNumberLabel.text = absoluteNumber;
} else {
_absoluteNumberLabel.text = @"(Valid regional number, but no given area code)";
}
_absoluteNumberLabel.textColor = [UIColor blackColor];
} else {
textField.textColor = [UIColor redColor];

_absoluteNumberLabel.text = @"(Not a valid number)";
_absoluteNumberLabel.textColor = [UIColor darkGrayColor];
}

// If these are the same then just let the normal text changing take place
if ([phone isEqualToString:txt]) {
Expand Down Expand Up @@ -141,12 +158,15 @@ - (void)viewDidLoad {

self.view.backgroundColor = [UIColor lightGrayColor];

UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(20, 40, self.view.frame.size.width - 40, 44)];
CGRect bgFrame = CGRectMake(20, 40, self.view.frame.size.width - 40, 44);
UIView *bg = [[UIView alloc] initWithFrame:bgFrame];
bg.autoresizingMask = UIViewAutoresizingFlexibleWidth;
bg.backgroundColor = [UIColor whiteColor];
bg.layer.cornerRadius = 10;

UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, bg.frame.size.width - 20, 24)];
// The text field where you type the number.
CGRect tfFrame = CGRectMake(10, 10, bg.frame.size.width - 20, 24);
UITextField *tf = [[UITextField alloc] initWithFrame:tfFrame];
tf.keyboardType = UIKeyboardTypePhonePad;
tf.backgroundColor = [UIColor whiteColor];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
Expand All @@ -159,6 +179,16 @@ - (void)viewDidLoad {
[self.view addSubview:bg];

_textField = tf;

// Label below the text field showing the absolute international number.
CGRect absoluteFrame = CGRectMake(bgFrame.origin.x,
bgFrame.origin.y + bgFrame.size.height + 10.0,
bgFrame.size.width,
16.0);
_absoluteNumberLabel = [[UILabel alloc] initWithFrame:absoluteFrame];
_absoluteNumberLabel.font = [UIFont systemFontOfSize:13.0];
_absoluteNumberLabel.backgroundColor = [UIColor clearColor];
[self.view addSubview:_absoluteNumberLabel];
}

- (void)viewWillAppear:(BOOL)animated {
Expand Down