ios - 语言改变了但 CLLocationManager 没有受到影响?

标签 ios xcode swift3

我正在使用本地化和 clocationManager

当更改语言时一切正常,只有国家和城市名称与以前的语言一起使用,本地化完全没有影响,唯一的方法是关闭应用程序并再次打开它以使用新语言! 在不关闭应用程序的情况下更改国家和城市的任何解决方案?

本地化代码

static func setLanguage(lang:String) -> Void {
    UserDefaults.standard.removeObject(forKey: "AppleLanguages")
    UserDefaults.standard.set([lang], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()

    Bundle.setLanguage(lang)
}

CLLocationManager代码

   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locValue = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")


    let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: locValue.latitude, longitude: locValue.longitude)

    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]

        if placeMark != nil{
            // City
            if let city = placeMark.addressDictionary!["State"] as? NSString {

                self.locCity = city as String
            }



            // Country
            if let country = placeMark.addressDictionary!["Country"] as? NSString {

                self.locCountry = "\(country)," as String
            }

            self.countryLocation.text = "\(self.locCountry) \(self.locCity)"
            self.locationManager.stopUpdatingLocation()
        }

    })
}

最佳答案

您必须在 NSLocale.currentLocaleDidChangeNotification 上添加观察者以检测任何此类更改。

像这样:

NotificationCenter.default.addObserver(self, selector: #selector(MyClass.languageDidChange),
                                               name: NSLocale.currentLocaleDidChangeNotification, object: nil)

languageDidChange 方法将在 NSLocale 发生变化时被调用。

希望这对您有所帮助。

关于ios - 语言改变了但 CLLocationManager 没有受到影响?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395467/

相关文章:

ios - 一个低框架的cocos2d程序

ios - UITabBarController 无法加载第二个选项卡

c++ - iPhone上C++开源游戏引擎推荐

ios - Xcode 6.3 上的可空性问题

ios - 解析推送通知框架中的 Apple Mach-O 链接器错误

ios - "Expected expression in list of expressions"

ios - Monotouch Ipad3 retina 无法以全分辨率运行

Swift Block 值错误

swift - 编辑 SKSpriteNode 自定义类的属性?

Swift 2.x 到 swift 3,XCode 报错 : Cannot convert value of type PHFetchResult