Giter Site home page Giter Site logo

rmaddy / rmphoneformat Goto Github PK

View Code? Open in Web Editor NEW
266.0 10.0 61.0 225 KB

RMPhoneFormat provides a simple to use class for formatting phone numbers in iOS apps. The formatting should replicate what you would see in the Contacts app for the same phone number.

Objective-C 100.00%

rmphoneformat's Introduction

#RMPhoneFormat

RMPhoneFormat provides a simple to use class for formatting and validating phone numbers in iOS apps. The formatting should replicate what you would see in the Contacts app for the same phone number.

The included sample project demonstrates how to use the formatting class to setup a text field that formats itself as the user types in a phone number. While the sample app is for iOS, the RMPhoneFormat class should work as-is under OS X.

##Setup

This class depends on a copy of an Apple provided private framework file named Default.phoneformat being copied into the app's resource bundle and named PhoneFormats.dat.

The Default.phoneformat file can be located in:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator<version>.sdk/System/Library/PrivateFrameworks/AppSupport.framework

where <version> is the version of the iPhone SDK installed. It is recommended that you use the one from 6.0.

Add RMPhoneFormat.m and RMPhoneFormat.h to your own project. These files use ARC. If your project is not using ARC then you must add the -fobjc-arc flag to the RMPhoneFormat.m file. Select your target in Xcode. Select the Build Phases tab. Open the Compile Sources pane. Select RMPhoneFormat.m and press Enter. Add -fobjc-arc in the popup window so it appears in the Compiler Flags column.

##Usage

In its simplest form you do the following:

RMPhoneFormat *fmt = [[RMPhoneFormat alloc] init];
// Call any number of times
NSString *numberString = // the phone number to format
NSString *formattedNumber = [fmt format:numberString];

You can also pass in a specific default country code if you don't want to rely on the Region Format setting. Pass in a valid ISO 3166-1 two-letter country code in lowercase:

RMPhoneFormat *fmt = [[RMPhoneFormat alloc] initWithDefaultCountry:@"uk"];
// Call any number of times
NSString *numberString = // the phone number to format
NSString *formattedNumber = [fmt format:numberString];

You may also use the singleton interface if desired:

RMPhoneFormat *fmt = [RMPhoneFormat instance];
// Call any number of times
NSString *numberString = // the phone number to format
NSString *formattedNumber = [fmt format:numberString];

To validate a phone number you can do the following:

RMPhoneFormat *fmt = [[RMPhoneFormat alloc] init];
// Call any number of times
NSString *numberString = // the phone number to validate
BOOL valid = [fmt isPhoneNumberValid:fmt];

The phone number to validate can include formatting characters or not. The number will be valid if there are an appropriate set of digits.

RMPhoneFormat can also be used to lookup a country's calling code:

RMPhoneFormat *fmt = [RMPhoneFormat instance];
NSString *callingCode = [fmt callingCodeForCountryCode:@"AU"]; // Australia - returns 61
NSString *defaultCallingCode = [fmt defaultCallingCode]; // based on current Region Format (locale)

##Notes

See the comments in RMPhoneFormat.m for additional details.

Please note that the format of the Default.phoneformat file is undocumented. There are aspects to this file that are not yet understood. This means that some phone numbers in some countries may not be formatted correctly.

##Issues

If you encounter an issue where a phone number is formatted differently with RMPhoneFormat than the Contacts app, please let me know. Be sure to provide the phone number, the output from RMPhoneFormat, the output shown in Contacts, and the Region Format setting from the Settings app.

##License Copyright (c) 2012, Rick Maddy All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

rmphoneformat's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rmphoneformat's Issues

Validation?

Good work! This is very interesting and useful class.

One quick question:
How can I check if a string is valid as a phone number?

I know how to format, but couldn't find how to validate.

Thanks

Missing dat file

i just downloaded the project and whenever i tries to run it says missing the dat file and it crashes. badly need help.

Crashing on launch

Hi there, I have followed the instructions and added the PhoneFormats.dat file, but the sample project throws an exception and crashes on launch for me on line 1078 of RMPhoneFormat.m, in the parseDataHandler method:

    [_countryCallingCode setObject:callingCode forKey:country];

Reason is "setObjectForKey: key can not be nil"

Any advice? Thanks!

Add CallingCodeInfo object to public interface

it would be cool if there was a way to grab the updated CallingCodeInfo object from an instance of RMPhoneFormat so the client can easily grab parsed information about the current string thats being processed.

I can make the change if you don't see anything architecturally incorrect

Formatting while editing phone number

I am trying to achieve UITextField with formatted phone number while user input progress.
My algo:

  1. Get only digits from phone field
  2. Format digits as phone number
  3. Set UITextFields text property new formatted phone number

All works fine, until user tries to edit phone number in the middle.
I can not properly save and restore selectedRange because of formatting chars " -()"

Is there any solution for this problem?

Validation for some international telephone numbers not working

Hi,

I'm glad I found this little utility of yours, am impressed by the "on-the-fly" formatting, but am having some issues with phone number validation for some countries.

My initial tests show that the validation doesn't work at all for Italy (valid number shows as invalid) or Germany (while typing the number it shows up valid at the halfway point but not when complete).

I used the PhoneFormats.dat file from the sdk9.3 simulator.

Would it help to try a different formats file or are there known issues with these numbers?

Thanks for any help. I'd really like to get this working!

AppStore Validation Issue

Hi,

Using this lib causes issue while validating a project for App Store submission. I believe it's a false positive but could you change the name of the method so it doesn't raise a flag?

Thanks!

d7crw

Austrian number not properly validated

I tried to validate/format the Austrian number +43 7226 3180. The formatting is as in the Contacts App. But the RMPhoneFormat tells me, that the number is invalid.

ios simulator screen shot 16 jan 2014 14 28 21

callingCodeForCountryCode returning NULL

  • Running this function on country codes from [NSLocale ISOCountryCodes]
  • I suspect the Default.phoneformat file could be missing something? I am getting the file from /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AppSupport.framework/
    RMPhoneFormat *format = [RMPhoneFormat instance];

    NSMutableArray *invalidCodes = [[NSMutableArray alloc] init];

    for (NSString *countryCode in [NSLocale ISOCountryCodes])
    {
        NSString *callingCode = [format callingCodeForCountryCode:countryCode];
        if (!callingCode) [invalidCodes addObject:countryCode];
    }

    NSLog(@"invalidCodes: %@", invalidCodes);

invalidCodes: (
    AX,
    BL,
    BQ,
    BV,
    CC,
    CW,
    CX,
    EH,
    GG,
    GS,
    HM,
    IM,
    JE,
    MF,
    NF,
    PN,
    SJ,
    SS,
    TF,
    TL,
    UM,
    YT
)

Indian phone number not properly formatted and validated

I tried to validate/format the Indian number (ex: +91 7702227774, +91 9966496003). But the formatting is not as in the Contacts App and also the RMPhoneFormat tells me, that the number is invalid. Please find below screenshots (BTW, green is valid and red color is not valid in below screenshots).

img_2393
ios simulator screen shot 19-nov-2014 3 04 18 pm
img_2394

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.