Code Monkey home page Code Monkey logo

weblineindia / swiftui-country-code-picker Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 1.26 MB

This Country Code Picker is a fully reusable demo or library that provides users with a convenient way to select and display country codes from a comprehensive list. It simplifies the process of selecting the correct country code and flag.

Home Page: https://www.weblineindia.com/software-development-resources.html

License: MIT License

Swift 100.00%
country-code-picker country-codes-component ios reusable-code reusable-components swift swiftui reusable-library

swiftui-country-code-picker's Introduction

SwiftUI Country Code Picker

This Country Code Picker is a fully reusable demo that provides users with a convenient way to select and display country codes from a comprehensive list. It simplifies the process of selecting the correct country code and thier flag as well.

Table of Contents

Demo ๐Ÿ“ฑ

This demo contains a screen which have country code selection and input field to enter mobile number.

Demo Normal Preview

Features ๐Ÿฅณ

App has main feature as:

  • Country code picker feature.
  • Demo has signle screen with country code selection and one input field of mobile number.
  • Multiselection flag available to select multiple country code.
  • Fully customization available.
  • Not any third party dependency.
  • Minimum iOS Target 16.0

Installation ๐Ÿ’พ

The instructions on how to install or run this demo:

  • Clone the repository.
  • Open the project in Xcode.
  • Build and run the project.

Usage & Integration ๐Ÿ‘ฉ๐Ÿพโ€๐Ÿ”ฌ

How to add the country code picker feature into your own SwiftUI project. follow below steps

** Create your new SwiftUI project with main app content view. ** Drag and drop folder name as CountryPickerSource source of files into your project. That contains all necessary files which needed for picker.

** CountryPickerRepresentable - This File is using for swiftUI to present the view controller. ** CountryModel - This file is using for JSON parsing of country list. ** CountryCodes - This is JSON file of country list. ** Countries - Array of country model object. ** CountriesViewController - This file using to present the screen and render country data. ** Country - Using to extract particular data as country name, code and flag by defining methods. ** CountriesViewControllerDelegate - This contains the delegate methods of CountriesViewController ** SwiftyJSON This is one type of helper which is using for parsing of json data.

Put the code into your initial content view:

Here integreated country picker on taping of a button and display data as flag and code after selection. And additionaly added one input field for mobile number.

struct ContentView: View {
@State private var selectedCountry: CountryModel?
@State private var isPickerVisible = false
@State private var mobile = ""

var body: some View {

VStack(alignment: .center, spacing: 50) {
Image("logo").frame(width: 200,height: 100,alignment: .center)
Text("Country Code Picker")
.font(.title)
.foregroundColor(.primary)
HStack {
Button(action: {
isPickerVisible.toggle()
}) {
HStack {
Text(selectedCountry?.countryFlag ?? "")
.frame( height: 20)

Text(selectedCountry?.countryCode ?? "CountryCode")
.foregroundColor(.gray)
}.padding(5)
}
Divider().background(Color.gray)
.frame(width: 1).padding(5)
TextField("Phone Number", text: $mobile)
}.frame(height: 50)
.background(Color.secondary.opacity(0.1))
.cornerRadius(8)
.padding(10)
Spacer()

}.sheet(isPresented: $isPickerVisible) {
CountryPicker(country: $selectedCountry)
}
}
}

** CountryPickerRepresentable This file can modify as per requirement and customise the feature, by default its single selection of picker but its support multiple selection.

struct CountryPicker: UIViewControllerRepresentable {
let countryPicker = CountriesViewController()
@Binding var country: CountryModel?

func makeUIViewController(context: Context) -> CountriesViewController {
countryPicker.allowMultipleSelection = false
countryPicker.delegate = context.coordinator
return countryPicker
}

func updateUIViewController(_ uiViewController: CountriesViewController, context: Context) {
}

func makeCoordinator() -> Coordinator {
return Coordinator(self)
}

class Coordinator: NSObject, CountriesViewControllerDelegate {
func countriesViewControllerDidCancel(_ countriesViewController: CountriesViewController) {
}

func countriesViewController(_ countriesViewController: CountriesViewController, didSelectCountry country: Country) {
let cModel = CountryModel()
if let info = getCountryAndName(country.countryCode) {
cModel.countryCode  = info.countryCode!
cModel.countryFlag  = info.countryFlag!
}
parent.country = cModel
}

func countriesViewController(_ countriesViewController: CountriesViewController, didUnselectCountry country: Country) {
}

func countriesViewController(_ countriesViewController: CountriesViewController, didSelectCountries countries: [Country]) {
}

var parent: CountryPicker
init(_ parent: CountryPicker) {
self.parent = parent
}
}
}

UIKit - Swift Integration

And also we can use same Source library for UIKit in swift project as well. Write below code to open country picker.

DispatchQueue.main.async {
CountriesViewController.show(countriesViewController: self.countriesViewController, toVar: self)
} 

Contributing

  • Created something awesome, made this code better, added some functionality, or whatever (this is the hardest part).
  • Fork it.
  • Create new branch to contribute your changes.
  • Commit all your changes to your branch.
  • Submit a pull request.

Need Help?

We also provide a free, basic support for all users who want to use this coordinator master demo in project. In case you want to customize this demo input to suit your development needs, then feel free to contact our iOS SwiftUI Developers.

Collection of Components

We have built many other components and free resources for software development in various programming languages. Kindly click here to view our Free Resources for Software Development.

Changelog

Detailed changes for each release are documented in CHANGELOG.

License ๐Ÿ“ƒ

This project is licensed under the MIT License. Feel free to use, modify, and distribute the code as permitted by the license.

Acknowledgements

This app was created using SwiftUI and leverages various libraries and resources. We would like to acknowledge and express our gratitude to the following:

  • SwiftUI: Apple's declarative framework for building user interfaces.
  • Xcode: Integrated development environment (IDE) provided by Apple.
  • Open-source libraries: Various open-source libraries and frameworks used in the project.
  • Community contributors: Contributors who have provided support, bug fixes, and enhancements.

Keywords

Country Picker Master, SwiftUI Country Code Picker, Country Picker Reusable Component, Country Code, Swift5, iOS 16, WeblineIndia

swiftui-country-code-picker's People

Contributors

partners-wli avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

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.