startMonitoringSignificantLocationChanges() 在 xcode 6 Beta2 中不能与 swift 一起使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24529327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
startMonitoringSignificantLocationChanges() not working with swift in xcode 6 Beta2?
提问by Martin Sj?blom
I'm trying to implement a more efficient background handling of my location dependent application and also learn some Swift while doing so.
我正在尝试对我的位置相关应用程序实现更有效的后台处理,并且在这样做的同时还学习了一些 Swift。
I noticed (the hard way) that I couldn't get locations to work at all in the first Beta but switching to Beta2 of xcode solved that part.
我注意到(艰难的方式)我在第一个 Beta 版中根本无法找到工作位置,但是切换到 xcode 的 Beta2 解决了这部分问题。
In the setup of my location handling I initiate a LocationManager with code like this (I want to be able to switch to high resolution and even start in high mode):
在我的位置处理设置中,我使用这样的代码启动一个 LocationManager(我希望能够切换到高分辨率,甚至以高模式启动):
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
locationManager.startUpdatingLocation();
And then handle switching with code like:
然后使用如下代码处理切换:
if (highResolutionSwitch.on) {
println("Switching to high resolution");
locationManager.stopMonitoringSignificantLocationChanges();
locationManager.startUpdatingLocation();
} else {
println("Switching to low resolution");
locationManager.stopUpdatingLocation();
locationManager.startMonitoringSignificantLocationChanges();
}
When in "high"-mode I receive locations in my didUpdateLocations()
method but NEVER anything in "low"-mode.
Is it the beta nature of the xcode/swift-environment or am I missing something?
在“高”模式下,我会在我的didUpdateLocations()
方法中接收位置,但在“低”模式下从不接收任何位置。这是 xcode/swift-environment 的 beta 性质还是我遗漏了什么?
回答by Martin Sj?blom
Problem found!
发现问题!
The problem was that the call to locationManager.requestWhenInUseAuthorization()
was not enough. This doesn't enable significant location changes.
Changing that line to locationManager.requestAlwaysAuthorization()
made at least the simulator to give me one location (although no new location when I changed the position).
问题是调用到locationManager.requestWhenInUseAuthorization()
是不够的。这不会启用显着的位置更改。改变那条线locationManager.requestAlwaysAuthorization()
至少让模拟器给我一个位置(虽然当我改变位置时没有新的位置)。
I will redeploy my app and see if it works IRL.
我将重新部署我的应用程序,看看它是否有效 IRL。
I also needed to reinstall the app on the simulator so that the "allow" question was given again.
我还需要在模拟器上重新安装应用程序,以便再次给出“允许”问题。