Code Monkey home page Code Monkey logo

Comments (2)

david-hoze avatar david-hoze commented on August 29, 2024

OK, I managed to find out how to do it, here is what I did if anyone else is interested.
formattedPhoneNumberSubstring takes a partial phone number string and formats it as the beginning of a properly formatted international number, e.g. "16463" turns to "+1 646-3".

NSString *formattedPhoneNumberSubstring(NSString *phoneNumber) {
    NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
    phoneNumber = [phoneUtil normalizeDigitsOnly:phoneNumber];
    NSString *nationalNumber;
    NSNumber *countryCode = [phoneUtil extractCountryCode:phoneNumber nationalNumber:&nationalNumber];
    if ([countryCode isEqualToNumber:@0])
        return phoneNumber;

    NSString *regionCode = [[phoneUtil regionCodeFromCountryCode:countryCode] objectAtIndex:0];
    NSString *paddedNationalNumber = [nationalNumber stringByPaddingToLength:15 withString:@"0" startingAtIndex:0];
    NSString *formatted;
    NSString *formattedSubstr;
    for (int i=0; i < paddedNationalNumber.length; i++) {
        NSError *error = nil;
        formattedSubstr = [phoneUtil format:[phoneUtil parse:[paddedNationalNumber substringToIndex:i] defaultRegion:regionCode error:&error]
                               numberFormat:NBEPhoneNumberFormatINTERNATIONAL error:&error];
        if (getExtraCharacters(formattedSubstr) > getExtraCharacters(formatted)) // extra characters means more formatted
            formatted = formattedSubstr;
    }

    // Preparing the buffer for phoneNumber
    unichar phoneNumberBuffer[phoneNumber.length+1];
    [phoneNumber getCharacters:phoneNumberBuffer range:NSMakeRange(0, phoneNumber.length)];

    // Preparing the buffer for formatted
    unichar formattedBuffer[formatted.length+1];
    [formatted getCharacters:formattedBuffer range:NSMakeRange(0, formatted.length)];

    int j=0;
    for(int i = 0; i < phoneNumber.length && j < formatted.length; i++) {
        while(formattedBuffer[j] != phoneNumberBuffer[i]) j++;
        j++;
    }

    return [formatted substringToIndex:j];
}

Also, if anyone's interested in ReactiveCocoa, here's how to do bind it to a UITextField:

RAC(self,phoneNumberTextField.text) =
[[self.phoneNumberTextField.rac_textSignal
  map:^NSString*(NSString *phoneNumber) {
      return formattedPhoneNumberSubstring(phoneNumber);
  }] deliverOn:[RACScheduler mainThreadScheduler]];

from libphonenumber-ios.

iziz avatar iziz commented on August 29, 2024

Sorry for late reply.
Phone number has various formatting rule for each country. Because, You must enter specific country code for formatting phone number. (International or not)

But, if client can't recognize the country code for some phone number.
Your solution will be helpful.

And, if you want to add that features to "libPhoneNumber-iOS".
Just create "pull request". :)
Thanks.

from libphonenumber-ios.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.