[英]Location update even when app is killed/terminated
I am trying to get location updates even in all states, even when app is killed/terminated/suspended.我试图在所有状态下获取位置更新,即使应用程序被终止/终止/暂停也是如此。 I have enabled background fetch in xcode and implemented the following code(used reference " Capture location in all states app ").我在 xcode 中启用了后台获取并实现了以下代码(使用参考“ 在所有状态应用程序中捕获位置”)。 But when i terminate the app it's giving a red line on class AppDelegate.但是当我终止应用程序时,它在类 AppDelegate 上给出了一条红线。 I do not understand what is the problem here.I have done this using the solution of the question " Getting location for an iOS app when it is in the background and even killed " here, But its not working in ios 9.Please help me out or tell me the other solution.我不明白这里的问题是什么。我已经使用问题的解决方案“ 获取 iOS 应用程序在后台甚至被杀死时的位置”问题来完成此操作,但它在 ios 9 中不起作用。请帮助我出来或告诉我其他解决方案。
UPDATED CODE -更新代码 -
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()
}
}
Check this tutorial: http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended查看本教程:http: //mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended
And the source code here: https://github.com/voyage11/GettingLocationWhenSuspended源代码在这里: https ://github.com/voyage11/GettingLocationWhenSuspended
Hope you will get your answer.希望你会得到你的答案。 It's working for me.它对我有用。 Now I am trying to make a http request when 'didUpdateLocations' function being called, so that I can store user current location in server when the app is not running(Suspended/terminated/killed).现在我正在尝试在调用“didUpdateLocations”函数时发出 http 请求,这样我就可以在应用程序未运行(暂停/终止/终止)时将用户当前位置存储在服务器中。
I don't think apple allowed to make any http request when the app is suspended/terminated.当应用程序暂停/终止时,我认为苹果不允许发出任何 http 请求。 But if server received the request of location update, then I am good to go with that.但是,如果服务器收到位置更新请求,那么我很乐意接受。 Because I don't need response back from server.因为我不需要服务器的响应。 Need a lot of testing....需要大量的测试......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.