[英]Location update even when app is killed/terminated
我试图在所有状态下获取位置更新,即使应用程序被终止/终止/暂停也是如此。 我在 xcode 中启用了后台获取并实现了以下代码(使用参考“ 在所有状态应用程序中捕获位置”)。 但是当我终止应用程序时,它在类 AppDelegate 上给出了一条红线。 我不明白这里的问题是什么。我已经使用问题的解决方案“ 获取 iOS 应用程序在后台甚至被杀死时的位置”问题来完成此操作,但它在 ios 9 中不起作用。请帮助我出来或告诉我其他解决方案。
更新代码 -
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
var significatLocationManager : CLLocationManager?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){
print("yessssss")
}else{
print("noooooo")
}
if let launchOpt = launchOptions{
if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) {
self.significatLocationManager = CLLocationManager()
self.significatLocationManager?.delegate = self
self.significatLocationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.significatLocationManager!.allowsBackgroundLocationUpdates = true
}
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startMonitoringSignificantLocationChanges()
}
}else{
self.locationManager = CLLocationManager()
self.locationManager?.delegate = self
self.locationManager?.requestAlwaysAuthorization()
if #available(iOS 9.0, *) {
self.locationManager!.allowsBackgroundLocationUpdates = true
}
self.locationManager?.startMonitoringSignificantLocationChanges()
}
return true
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let locationArray = locations as NSArray
let locationObj = locationArray.lastObject as! CLLocation
let coord = locationObj.coordinate
}
func applicationDidEnterBackground(application: UIApplication) {
if self.significatLocationManager != nil {
self.significatLocationManager?.startMonitoringSignificantLocationChanges()
}else{
self.locationManager?.startMonitoringSignificantLocationChanges()
}
}
查看本教程:http: //mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended
源代码在这里: https ://github.com/voyage11/GettingLocationWhenSuspended
希望你会得到你的答案。 它对我有用。 现在我正在尝试在调用“didUpdateLocations”函数时发出 http 请求,这样我就可以在应用程序未运行(暂停/终止/终止)时将用户当前位置存储在服务器中。
当应用程序暂停/终止时,我认为苹果不允许发出任何 http 请求。 但是,如果服务器收到位置更新请求,那么我很乐意接受。 因为我不需要服务器的响应。 需要大量的测试......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.