Closed hcancelik closed 2 years ago
Hello @hcancelik . I've release v1.0.2 fixing accessibility of getCountries
.
For the 2nd part of your question. Can you please give details what do you mean by "way to get Country"?
Hi @Rashidium
Thanks for the update.
What I mean by getting the Country is basically an initializer in Country. Something like this:
public init(regionCode _regionCode: String) {
self.isoCode = _regionCode
if let country = CountryManager.shared.getCountries().first(where: { $0.isoCode == _regionCode }) {
self.phoneCode = country.phoneCode
} else {
self.phoneCode = ""
}
}
Since there is no way to get the phone code with iOS, this will help anyone to set a default value of the picker by using the Locale.current.region
@hcancelik have you checked the CountryPickerViewController.selectedCountry
? It can be used to set selected country by country code.
@hcancelik have you checked the
CountryPickerViewController.selectedCountry
? It can be used to set selected country by country code.
Yes I did. I might be doing something wrong but it doesn't provide a default value for the picker in swiftui. It only highlights the country in the list view.
can you please give any detail about highlighting? Do you mean selected background color under country code?
Also, could you please set selectedCountry
in func updateUIViewController(_ uiViewController: CountryPickerViewController, context: Context)
function?
@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.
https://user-images.githubusercontent.com/5788410/147405630-19fab385-5554-43b2-a7a8-2deaaef4c924.mp4
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")
Ok I see what you meant now 👍🏻 Thanks for details. Though your solution would work, I think there could be better way to init Country without need to isoCode. I think we better open a pull request for the request you made.
I'm getting this error when I try to use getCountries method on CountryManager singleton.
Any idea how I can fix this?
Additionally is there a way to get Country without providing phone code and isoCode?
Thanks in advance.