mobven / countrypicker Goto Github PK
View Code? Open in Web Editor NEWCountry Picker with unicode flags and country codes.
License: MIT License
Country Picker with unicode flags and country codes.
License: MIT License
Hello mates, could you please to update the license to use your library?
I love your library. It may help me save the time.
Thank you very much <3
Here is an example of UIViewControllerRepresentable that works with SwiftUI.
Please feel free to add this to docs.
//
// CountryPicker.swift
//
import SwiftUI
import CountryPicker
struct CountryPicker: UIViewControllerRepresentable {
@Binding var country: Country?
typealias UIViewControllerType = CountryPickerViewController
let countryPicker = CountryPickerViewController()
func makeUIViewController(context: Context) -> CountryPickerViewController {
countryPicker.selectedCountry = Locale.current.regionCode ?? "US"
countryPicker.delegate = context.coordinator
return countryPicker
}
func updateUIViewController(_ uiViewController: CountryPickerViewController, context: Context) {
//
}
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
class Coordinator: NSObject, CountryPickerDelegate {
var parent: CountryPicker
init(_ parent: CountryPicker) {
self.parent = parent
}
func countryPicker(didSelect country: Country) {
parent.country = country
}
}
}
Example Usage:
//
// ContentView.swift
//
import SwiftUI
import CountryPicker
struct ContentView: View {
@State private var country: Country?
@State private var showCountryPicker = false
var body: some View {
VStack {
Button {
showCountryPicker = true
} label: {
Text("Select Country")
}.sheet(isPresented: $showCountryPicker) {
CountryPicker(country: $country)
}
}
}
}
I don't see for now a way to hide the phone numbers from the list view.
Can you also add that please?
There are cases when i only need the flag and the country, but showing the phone number is not necessary.
@Rashidium What I meant was the set a default selected country for the picker.
Right now, I have set selectedCountry
to TR which sets a green background in the picker view but this is not enough to pre-select the value.
import SwiftUI
import CountryPicker
struct ContentView: View {
@State private var country: Country?
@State private var showCountryPicker = false
var body: some View {
VStack {
Button {
showCountryPicker = true
} label: {
Text("Select Country")
}.sheet(isPresented: $showCountryPicker) {
CountryPicker(country: $country)
}
Text("Selected Country: \(country?.localizedName ?? "N/A")")
}
}
}
Since with iOS, I can only get the Locale region, I thought it would be better to have a initializer in Country struct to set this default value.
public init(regionCode: String) {
self.isoCode = regionCode
if let country = CountryManager.shared.getCountries().first(where: { $0.isoCode == regionCode }) {
self.phoneCode = country.phoneCode
} else {
self.phoneCode = ""
}
}
This way I can set @State private var country = Country(regionCode: Locale.current.regionCode ?? "TR")
Originally posted by @hcancelik in #6 (comment)
Reuquest for a feature where a specified list of countries was on the top of list.
fx
let countryPicker = CountryPickerViewController()
countryPicker.preferedCountries = ["DK", "SE", "NO"]
struct CountryPicker : UIViewControllerRepresentable{
typealias UIViewControllerType = CountryPickerViewController
let countryPicker = CountryPickerViewController()
func makeUIViewController(context: Context) -> CountryPickerViewController {
countryPicker.selectedCountry = "TR"
return countryPicker
}
func updateUIViewController(_ uiViewController: CountryPickerViewController, context: Context) {
}
func makeCoordinator() -> Coordinator {
return Coordinator(countryPicker)
}
class Coordinator : NSObject , CountryPickerDelegate{
init(_ parent : CountryPickerViewController) {
super.init()
parent.delegate = self
}
func countryPicker(didSelect country: Country) {
print(country)
}
}
}
When using this plugin on my swiftui app on the last page, when the country list open, the navigationview pop to the root view. any ideas. thanks
When I select country from search result I got this error
Swift/ContiguousArrayBuffer.swift:575: Fatal error: Index out of range
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.