Giter Site home page Giter Site logo

Comments (7)

Rashidium avatar Rashidium commented on September 2, 2024

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"?

from countrypicker.

hcancelik avatar hcancelik commented on September 2, 2024

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

from countrypicker.

Rashidium avatar Rashidium commented on September 2, 2024

@hcancelik have you checked the CountryPickerViewController.selectedCountry? It can be used to set selected country by country code.

from countrypicker.

hcancelik avatar hcancelik commented on September 2, 2024

@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.

from countrypicker.

Rashidium avatar Rashidium commented on September 2, 2024

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?

from countrypicker.

hcancelik avatar hcancelik commented on September 2, 2024

@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.

Simulator.Screen.Recording.-.iPhone.11.Pro.Max.-.2021-12-26.at.13.33.06.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")

from countrypicker.

Rashidium avatar Rashidium commented on September 2, 2024

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.

from countrypicker.

Related Issues (11)

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.