Ios 语言更改,但CLLocationManager没有更改';没有影响?
我正在使用本地化和cllocationManager 当更改语言时,一切都很好,只是国家和城市名称与以前的语言一起出现,而不受本地化的影响,唯一的方法是关闭应用程序并再次打开它以使用新语言!! 有没有解决方案可以在不关闭应用程序的情况下更改国家和城市 本地化代码Ios 语言更改,但CLLocationManager没有更改';没有影响?,ios,xcode,swift3,Ios,Xcode,Swift3,我正在使用本地化和cllocationManager 当更改语言时,一切都很好,只是国家和城市名称与以前的语言一起出现,而不受本地化的影响,唯一的方法是关闭应用程序并再次打开它以使用新语言!! 有没有解决方案可以在不关闭应用程序的情况下更改国家和城市 本地化代码 static func setLanguage(lang:String) -> Void { UserDefaults.standard.removeObject(forKey: "AppleLanguages")
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.currentLocaleEdidChangeNotification
上添加一个观察者来检测任何此类更改
大概是这样的:
NotificationCenter.default.addObserver(self, selector: #selector(MyClass.languageDidChange),
name: NSLocale.currentLocaleDidChangeNotification, object: nil)
而languageDidChange
方法将在NSLocale
中发生更改后被调用
希望这有帮助。我尝试过,但也是一样,问题不在于语言的错误更改问题在于CLLocationManager没有接受任何语言参数,需要重新启动app@MOMMH您是否尝试过重新初始化
CLLocationManager
?没有,但我不知道如何重新初始化CLLocationManager,有任何示例代码或链接吗?重新初始化意味着您只需创建CLLocationManager
的新对象。类似这样的内容:self.locationManager=CLLocationManager()
不幸的是,在赋值为零并重新初始化之后,我几乎完成了所有操作,但没有生效,非常感谢您的帮助