【发布时间】:2017-05-08 03:16:35
【问题描述】:
我写了这个程序,当应用程序显示在屏幕上时它就可以工作
如果应用程序在后台,它会停止打印纬度,当我恢复应用程序时,它会重新开始打印。
我在 xcode 中启用了后台模式,还检查了Location updates,为什么我的应用仍然没有在后台运行?
如果应用程序正在运行,只是print函数在后台不起作用,我怎么知道应用程序正在运行?
class ViewController: UIViewController,
CLLocationManagerDelegate {
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation])
{
let latestLocation: CLLocation = locations[locations.count - 1]
print(latestLocation.coordinate.latitude)
}
override func viewDidLoad() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
startLocation = nil
}
}
【问题讨论】:
-
您只请求了“使用时”权限,因此您的应用在后台时不会获取位置。为此,您需要请求“始终”权限
-
现在我使用
requestAlwaysAuthorization(),它仍然没有在后台运行 -
您是否将所需的使用字符串添加到您的 info.plist 中?您是否因“总是”许可而被提升?当您的应用暂停时,此视图控制器是否处于活动状态?
标签: ios swift cocoa-touch cllocation background-mode