SkillAgentSearch skills...

CountryPickerView

A simple, customizable view for efficiently collecting country information in iOS apps.

Install / Use

/learn @kizitonwose/CountryPickerView
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

CountryPickerView

Build Status Platform Version Carthage compatible SPM compatible License

CountryPickerView is a simple, customizable view for selecting countries in iOS apps.

You can clone/download the repository and run the demo project to see CountryPickerView in action. First run pod install from the CountryPickerViewDemo directory.

<img align="left" src="https://raw.githubusercontent.com/kizitonwose/CountryPickerView/master/CountryPickerViewDemo/Screenshots/1.png" width="300"> <img src="https://raw.githubusercontent.com/kizitonwose/CountryPickerView/master/CountryPickerViewDemo/Screenshots/2.png" width="300"> <img align="left" src="https://raw.githubusercontent.com/kizitonwose/CountryPickerView/master/CountryPickerViewDemo/Screenshots/3.png" width="300"> <img src="https://raw.githubusercontent.com/kizitonwose/CountryPickerView/master/CountryPickerViewDemo/Screenshots/4.png" width="300">

Installation

Note that 3.x releases are Swift 5 compatible. For the Swift 4 compatibility, use 2.x releases. For the Swift 3 compatibility, use 1.x releases.

Swift Package Manager

Swift Package Manager is a dependency manager built into Xcode.

If you are using Xcode 11 or higher, go to File / Swift Packages / Add Package Dependency… and enter package repository URL https://github.com/kizitonwose/CountryPickerView.git then follow the instructions.

Cocoapods

CountryPickerView is available through CocoaPods. Simply add the following to your Podfile:

use_frameworks!

target '<Your Target Name>' do
  pod 'CountryPickerView'
end

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

To install CountryPickerView through Carthage, simply add the following to your Cartfile:

github "kizitonwose/CountryPickerView"

Manual

  1. Put CountryPickerView repo somewhere in your project directory.
  2. In Xcode, add CountryPickerView.xcodeproj to your project.
  3. On your app's target, add the CountryPickerView framework:
    1. as an embedded binary on the General tab.
    2. as a target dependency on the Build Phases tab.

Usage

If you're using Storyboards/Interface Builder you can create a CountryPickerView instance by adding a UIView to your Storyboard, and then manually changing the view's class to CountryPickerView in the "Custom Class" field of the Identity Inspector tab on the Utilities panel (the right-side panel)

You can also create an instance of CountryPickerView programmatically:

import CountryPickerView

let cpv = CountryPickerView(frame: /**Desired frame**/)

To get the selected country from your CountryPickerView instance at any time, use the selectedCountry property.

let country = cpv.selectedCountry
print(country)

This property is not optional, the default value is the user's current country, derived from the device's current Locale.

Customization

Customization options for the view itself are available directly via the CountryPickerView instance while options for the internal CountryPicker table view are available via the CountryPickerViewDataSource protocol. Setting the CountryPickerViewDelegate protocol is also necessary if you wish to be notified when the user selects a country from the list.

import CountryPickerView

class DemoViewController: UIViewController, CountryPickerViewDelegate, CountryPickerViewDataSource {

    @IBOutlet weak var countryPickerView: CountryPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        countryPickerView.delegate = self
        countryPickerView.dataSource = self
    }
}

CountryPickerView properties

|Property|Description|Default value| |:-:|:-:|:-:| |showCountryCodeInView|Show or hide the country code(e.g NG) on the view.|true| |showPhoneCodeInView|Show or hide the phone code(e.g +234) on the view.|true| |showCountryNameInView|Show or hide the country name(e.g Nigeria) on the view.|false| |font|The font of the phone/country code text.|system font| |textColor|The color of the phone/country code text.|black| |flagSpacingInView|The spacing between the flag image and the phone code text.|8px| |hostViewController|The view controller used to show the internal CountryPickerViewController. If this is an instance of UINavigationController, the CountryPickerViewController will be pushed on the stack. If not, the CountryPickerViewController will be presented on its own navigation stack. If this property is nil, the view will try to find the nearest view controller and use that to present or push the CountryPickerViewController.|nil| |delegate|An instance of CountryPickerViewDelegate type.|nil| |dataSource|An instance of CountryPickerViewDataSource type.|nil|

Note: The properties showCountryCodeInView and showCountryNameInView can't both be enabled at the same time. Enabling one to will disable the other. You can only show all properties on the list(see CountryPickerViewDataSource).

CountryPickerViewDelegate

  • Called when the user selects a country from the list or when you manually set the selectedCountry property of the CountryPickerView

    func countryPickerView(_ countryPickerView: CountryPickerView, didSelectCountry country: Country)
    
  • Called before the CountryPickerViewController is presented or pushed. The CountryPickerViewController is a UITableViewController subclass.

    func countryPickerView(_ countryPickerView: CountryPickerView, willShow viewController: CountryPickerViewController)
    
  • Called after the CountryPickerViewController is presented or pushed. The CountryPickerViewController is a UITableViewController subclass.

    func countryPickerView(_ countryPickerView: CountryPickerView, didShow viewController: CountryPickerViewController)
    

Note: If you already have a Country class or struct in your project then implementing the didSelectCountry delegate method can cause a compile error with a message saying that your conforming class does not comform to the CountryPickerViewDelegate protocol. This is because Xcode can't figure out which Country model to use in the method. The solution is to replace the Country in the method signature with the typealias CPVCountry, your delegate method should now look like this:

func countryPickerView(_ countryPickerView: CountryPickerView, didSelectCountry country: CPVCountry)

You can also use CPVCountry as a replacement for the framework's Country model in other parts of your project.

Also, willShow and didShow delegate methods are optional. If the CountryPickerViewController is presented(not pushed), it is embedded in a UINavigationController. The CountryPickerViewController class is made available so you can customize its appearance if needed. You can also access the public searchController(UISearchController) property in the CountryPickerViewController for customization.

CountryPickerViewDataSource

The datasource methods define the internal(country list) ViewController's behavior. Run the demo project to play around with the options. All methods are optional.

  • An array of countries you wish to show at the top of the list. This is useful if your app is targeted towards people in specific countries.

    func preferredCountries(in countryPickerView: CountryPickerView) -> [Country]
    
  • The desired title for the preferred section.

    func sectionTitleForPreferredCountries(in countryPickerView: CountryPickerView) -> String?
    

    Note: You have to return a non-empty array of countries from preferredCountries(in countryPickerView: CountryPickerView) as well as this section title if you wish to show preferred countries on the list. Returning only the array or title will not work.

  • Show ONLY the preferred countries section on the list. Default value is false

    func showOnlyPreferredSection(in countryPickerView: CountryPickerView) -> Bool
    

    Return true to hide the internal list so your users can only choose from the preferred countries list.

  • The desired font for the section title labels on the list. Can be used to configure the text size.

    func sectionTitleLabelFont(in countryPickerView: CountryPickerView) -> UIFont
    
  • The desired text color for the section title labels on the list.

    func sectionTitleLabelColor(in countryPickerView: CountryPickerView) -> UIColor?
    
  • The desired font for the cell labels on the list. Can be used to configure the text size.

    func cellLabelFont(in countryPickerView: CountryPickerView) -> UIFont
    
  • The desired text color for the country names on the list.

    func cellLabelColor(in countryPickerView: CountryPickerView) -> UIColor?
    
  • The desired size for the flag images on the list.

    func cellImageViewSize(in countryPickerView: CountryPickerView) -> CGSize
    
  • The desired corner radius for the flag

View on GitHub
GitHub Stars514
CategoryDevelopment
Updated16d ago
Forks203

Languages

Swift

Security Score

100/100

Audited on Mar 17, 2026

No findings