- iOS 12.0+ / macOS 14.4+
- Xcode 15.3+
- Swift 5+
JNPhoneNumberView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'JNPhoneNumberView'
To run the example project, clone the repo, and run pod install
from the Example directory first.
used to to show the country dial code and the phone number, you can click on the dial code and select another country from the countries picker, this view has a delegate methods to pass the international number and validity of it.
-
-
Drag an UIView and change the class to "JNPhoneNumberView"
-
Add refrence for it in the view controller.
-
-
Set the 'delegate' in your view controller and implement the following methods:
-
Presenter View Controller to be used for presenting the Country list picker:
func phoneNumberView(getPresenterViewControllerFor phoneNumberView: JNPhoneNumberView) -> UIViewController
-
Get Country picker configuration:
func phoneNumberView(getCountryPickerAttributesFor phoneNumberView: JNPhoneNumberView) -> JNCountryPickerConfiguration
-
Did change text with new national phone number and selected country:
func phoneNumberView(didChangeText nationalNumber: String, country: JNCountry, forPhoneNumberView phoneNumberView: JNPhoneNumberView)
-
Did end editing with bool value indicate if the phone number is valid, new national phone number and selected country:
func phoneNumberView(didEndEditing nationalNumber: String, country: JNCountry, isValidPhoneNumber: Bool, forPhoneNumberView phoneNumberView: JNPhoneNumberView)
-
Selected Country did changed with bool value indicate if the phone number is valid:
func phoneNumberView(countryDidChanged country: JNCountry, isValidPhoneNumber: Bool, forPhoneNumberView phoneNumberView: JNPhoneNumberView)
-
-
Set dataSourceDelegate in your view controller if you want to provide a source with custom countries instead of using the our like the following:
func countryPickerViewControllerLoadCountryList(_ phoneNumberView: JNPhoneNumberView, completion: @escaping ([JNCountry]) -> Void, errorCompletion: @escaping (NSError) -> Void)
-
We provide appearance customization using JNPhoneNumberViewConfiguration that has the following attributtes:
- countryDialCodeTitleColor Color for country dial code.
- countryDialCodeTitleFont: Font for country dial code.
- phoneNumberTitleColor: Color for phone number textfield.
- phoneNumberTitleFont: Font for phone number textfield.
- phoneNumberPlaceHolder: Place hodler for phone number textfield.
- leftToolBarBarButtonItemTitle: Title for Tool BarBar ButtonItem that appear of Keyboard.
- maximumNumbrOfDigits: Maximum number of digits allowed to add. the defualt value is 30 digit
-
Set a default country using this method, you just have to pass a country code such as "US", "PS":
func setDefaultCountryCode(defaultCountryCode: String)
-
Set View configuration to customization view appearance:
func setViewConfiguration(_ configuration: JNPhoneNumberViewConfiguration)
-
Set phone number in JNPhoneNumberView:
func setPhoneNumber(phoneNumber: String)
-
Set phone number from national number and country:
func setPhoneNumber(nationalNumber: String, preferredRegionCode: String)
-
Get phone number from JNPhoneNumberView:
func getPhoneNumber() -> String
-
Is phone number valid:
func isValidPhoneNumber() -> Bool
-
Get National Phone Number:
func getNationalPhoneNumber() -> String
-
Get dial code:
func getDialCode() -> String
- delegate : Picker Delegate
- dataSourceDelegate: Data Source Delegate
used to to show the countries list and select one of the countries, this view controller has a delegate methods to pass the selected country as JNCountry, also we provide the developer the flexiability to pass a custom country list insead of use the cached one.
-
Initiate JNCountryPickerViewController.
let countryPickerViewController = JNCountryPickerViewController()
-
Emped the view controller in navigation controller.
let nevigationController = UINavigationController(rootViewController: countryPickerViewController)
-
Present the navigation controller modally.
self.present(nevigationController, animated: true, completion: nil)
-
Set the 'delegate' in your view controller and implement the following methods:
- Did select Country
func countryPickerViewController(_ controller: JNCountryPickerViewController, didSelectCountry country: JNCountry)
-
Set the 'dataSourceDelegate' in your view controller and implement the following methods:
- Load country list from custom source
func countryPickerViewControllerLoadCountryList(_ controller: JNCountryPickerViewController, completion: @escaping ([JNCountry]) -> Void, errorCompletion: @escaping (NSError) -> Void)
-
We provide appearance customization using JNCountryPickerConfiguration that has the following attributtes:
- selectedTitleFont Selected item title font.
- titleFont Not selected item title font.
- selectedTitleColor Selected item title color.
- titleColor Not selected item title color.
- emptySearchMessageFont Message for empty search result.
- emptySearchMessageColor Color for empty search result message.
- searchBarTintColor Search bar tint color.
- navigationBarColor Navigation bar color.
- naigationBarTintColor Navigation bar tint color.
- navigationBarTitle Navigation bar Title.
- navigationBarTitleTextAttributes Navigation bar title text attributtes.
- selectBarButtonTitle Select bar button title.
- loadingAcivityIndicatorColor Loading acivity indicator color.
- emptySearchMessage Message for empty search result.
- emptySearchImage Image for empty search result.
- viewBackgroundColor View controller background color.
- pickerLanguage Picker language.
- tableCellInsets Cell margins.
- tableCellCornerRaduis Cell corner raduis.
- tableCellBackgroundColor Cell background color.
- showDialCode Show country dial code to appear after country name.
- pickerConfiguration: you can set a custom configuration instead of the default configuration as described in view customization section.
- selectedCountry: you can set country to be selected when picker opened.
- delegate : Picker Delegate
- dataSourceDelegate: Data Source Delegate
-
Custom country entitiy must conform to JNCountry protocol that has the following:
- code Country code.
- name Country name.
- dialCode Country dial code.
The Country Data should Follow ISO 3166-1
Hamzeh Khanfar & Jayel Zaghmoutt
JNPhoneNumberView is available under the MIT license. See the LICENSE file for more info.