我尝试对依赖于位置的应用程序实施更有效的后台处理,并在这样做时学习一些Swift。
我注意到(很难)我在第一个Beta中无法获得位置,但是切换到xcode的Beta2解决了这个问题。
在我的位置处理设置中,我使用这样的代码启动一个LocationManager(我希望能够切换到高分辨率甚至以高模式启动):
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
locationManager.startUpdatingLocation();
然后使用以下代码处理切换:
if (highResolutionSwitch.on) {
println("Switching to high resolution");
locationManager.stopMonitoringSignificantLocationChanges();
locationManager.startUpdatingLocation();
} else {
println("Switching to low resolution");
locationManager.stopUpdatingLocation();
locationManager.startMonitoringSignificantLocationChanges();
}
在" high" -mode中,我在didUpdateLocations()
方法中接收位置,但从来没有在"低" -mode中接收任何内容。
它是xcode / swift环境的beta性质还是我错过了什么?
答案 0 :(得分:10)
发现问题!
问题在于对locationManager.requestWhenInUseAuthorization()
的调用还不够。这不会启用重要的位置更改。
将该行更改为locationManager.requestAlwaysAuthorization()
至少使模拟器为我提供一个位置(虽然我改变位置时没有新位置)。
我将重新部署我的应用程序,看看它是否有效。
我还需要在模拟器上重新安装应用程序,以便再次提出“允许”问题。