https://github.com/monzo/libphonenumber-ios
https://github.com/monzo/libphonenumber-ios
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/monzo/libphonenumber-ios
- Owner: monzo
- License: apache-2.0
- Created: 2022-12-13T17:59:32.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-14T11:07:27.000Z (over 3 years ago)
- Last Synced: 2025-03-11T08:04:20.548Z (over 1 year ago)
- Language: Objective-C
- Size: 14.4 MB
- Stars: 1
- Watchers: 16
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Authors: AUTHORS
Awesome Lists containing this project
README
[](http://cocoapods.org/?q=libPhoneNumber-iOS)
[](http://cocoapods.org/?q=libPhoneNumber-iOS)
[](https://travis-ci.org/iziz/libPhoneNumber-iOS)
[](https://coveralls.io/github/iziz/libPhoneNumber-iOS?branch=master)
[](https://github.com/Carthage/Carthage)
# **libPhoneNumber for iOS**
- NBPhoneNumberUtil
- NBAsYouTypeFormatter
> ARC only
## Update Log
[https://github.com/iziz/libPhoneNumber-iOS/wiki/Update-Log](https://github.com/iziz/libPhoneNumber-iOS/wiki/Update-Log)
## Issue
You can check phone number validation using below link.
https://rawgit.com/googlei18n/libphonenumber/master/javascript/i18n/phonenumbers/demo-compiled.html
Please report, if the above results are different from this iOS library.
Otherwise, please create issue to following link below to request additional telephone numbers formatting rule.
https://github.com/google/libphonenumber/issues
Metadata in this library was generated from that. so, you should change it first. :)
## Install
### [Swift Package Manager](https://www.swift.org/package-manager/)
Add Swift Package `https://github.com/monzo/libPhoneNumber-iOS`
Exact version `1.0.3`
## Usage - **NBPhoneNumberUtil**
```obj-c
NBPhoneNumberUtil *phoneUtil = [NBPhoneNumberUtil sharedInstance];
NSError *anError = nil;
NBPhoneNumber *myNumber = [phoneUtil parse:@"6766077303"
defaultRegion:@"AT" error:&anError];
if (anError == nil) {
NSLog(@"isValidPhoneNumber ? [%@]", [phoneUtil isValidNumber:myNumber] ? @"YES":@"NO");
// E164 : +436766077303
NSLog(@"E164 : %@", [phoneUtil format:myNumber
numberFormat:NBEPhoneNumberFormatE164
error:&anError]);
// INTERNATIONAL : +43 676 6077303
NSLog(@"INTERNATIONAL : %@", [phoneUtil format:myNumber
numberFormat:NBEPhoneNumberFormatINTERNATIONAL
error:&anError]);
// NATIONAL : 0676 6077303
NSLog(@"NATIONAL : %@", [phoneUtil format:myNumber
numberFormat:NBEPhoneNumberFormatNATIONAL
error:&anError]);
// RFC3966 : tel:+43-676-6077303
NSLog(@"RFC3966 : %@", [phoneUtil format:myNumber
numberFormat:NBEPhoneNumberFormatRFC3966
error:&anError]);
} else {
NSLog(@"Error : %@", [anError localizedDescription]);
}
NSLog (@"extractCountryCode [%@]", [phoneUtil extractCountryCode:@"823213123123" nationalNumber:nil]);
NSString *nationalNumber = nil;
NSNumber *countryCode = [phoneUtil extractCountryCode:@"823213123123" nationalNumber:&nationalNumber];
NSLog (@"extractCountryCode [%@] [%@]", countryCode, nationalNumber);
```
##### Output
```
2014-07-06 12:39:37.240 libPhoneNumberTest[1581:60b] isValidPhoneNumber ? [YES]
2014-07-06 12:39:37.242 libPhoneNumberTest[1581:60b] E164 : +436766077303
2014-07-06 12:39:37.243 libPhoneNumberTest[1581:60b] INTERNATIONAL : +43 676 6077303
2014-07-06 12:39:37.243 libPhoneNumberTest[1581:60b] NATIONAL : 0676 6077303
2014-07-06 12:39:37.244 libPhoneNumberTest[1581:60b] RFC3966 : tel:+43-676-6077303
2014-07-06 12:39:37.244 libPhoneNumberTest[1581:60b] extractCountryCode [82]
2014-07-06 12:39:37.245 libPhoneNumberTest[1581:60b] extractCountryCode [82] [3213123123]
```
#### with Swift
##### Case (1) with Framework
```
import libPhoneNumberiOS
```
##### Case (2) with Bridging-Header
```obj-c
// Manually added
#import "NBPhoneNumberUtil.h"
#import "NBPhoneNumber.h"
// CocoaPods (check your library path)
#import "libPhoneNumber_iOS/NBPhoneNumberUtil.h"
#import "libPhoneNumber_iOS/NBPhoneNumber.h"
// add more if you want...
```
##### Case (3) with CocoaPods
import libPhoneNumber_iOS
##### - in swift class file
###### 2.x
```swift
override func viewDidLoad() {
super.viewDidLoad()
guard let phoneUtil = NBPhoneNumberUtil.sharedInstance() else {
return
}
do {
let phoneNumber: NBPhoneNumber = try phoneUtil.parse("01065431234", defaultRegion: "KR")
let formattedString: String = try phoneUtil.format(phoneNumber, numberFormat: .E164)
NSLog("[%@]", formattedString)
}
catch let error as NSError {
print(error.localizedDescription)
}
}
```
## Usage - **NBAsYouTypeFormatter**
```obj-c
NBAsYouTypeFormatter *f = [[NBAsYouTypeFormatter alloc] initWithRegionCode:@"US"];
NSLog(@"%@", [f inputDigit:@"6"]); // "6"
NSLog(@"%@", [f inputDigit:@"5"]); // "65"
NSLog(@"%@", [f inputDigit:@"0"]); // "650"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2"
NSLog(@"%@", [f inputDigit:@"5"]); // "650 25"
NSLog(@"%@", [f inputDigit:@"3"]); // "650 253"
// Note this is how a US local number (without area code) should be formatted.
NSLog(@"%@", [f inputDigit:@"2"]); // "650 2532"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 22"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 222"
NSLog(@"%@", [f inputDigit:@"2"]); // "650 253 2222"
// Can remove last digit
NSLog(@"%@", [f removeLastDigit]); // "650 253 222"
NSLog(@"%@", [f inputString:@"16502532222"]); // 1 650 253 2222
```
## libPhoneNumberGeocoding
For more information on libPhoneNumberGeocoding and its usage, please visit [libPhoneNumberGeocoding](https://github.com/iziz/libPhoneNumber-iOS/blob/master/libPhoneNumberGeocoding/README.md) for more information.
## libPhoneNumberShortNumber
For more information on libPhoneNumberShortNumber and its usage, please visit [libPhoneNumberShortNumber](https://github.com/iziz/libPhoneNumber-iOS/blob/master/libPhoneNumberShortNumber/README.md) for more information.
##### Visit [libphonenumber](https://github.com/google/libphonenumber) for more information or mail (zen.isis@gmail.com)