diff --git a/RMPhoneFormat/RMPhoneFormat.h b/RMPhoneFormat/RMPhoneFormat.h index 6aa5107..c594aca 100644 --- a/RMPhoneFormat/RMPhoneFormat.h +++ b/RMPhoneFormat/RMPhoneFormat.h @@ -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; diff --git a/RMPhoneFormat/RMPhoneFormat.m b/RMPhoneFormat/RMPhoneFormat.m index 803dfe8..2fa2ebc 100644 --- a/RMPhoneFormat/RMPhoneFormat.m +++ b/RMPhoneFormat/RMPhoneFormat.m @@ -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]; diff --git a/RMPhoneFormat/RMViewController.m b/RMPhoneFormat/RMViewController.m index c3636c9..0abff7f 100644 --- a/RMPhoneFormat/RMViewController.m +++ b/RMPhoneFormat/RMViewController.m @@ -34,6 +34,7 @@ @implementation RMViewController { RMPhoneFormat *_phoneFormat; NSMutableCharacterSet *_phoneChars; UITextField *_textField; + UILabel *_absoluteNumberLabel; } - (id)init { @@ -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]) { @@ -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; @@ -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 {