简体   繁体   English

即使应用程序被杀死/终止,位置更新

[英]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.

相关问题 应用程序被终止/终止时的连续位置详细信息 - Continuous location details when app is killed/Terminated 应用终止时更新位置 - Update Location when app is terminated 当应用程序被杀死/终止/挂起时,cordova获取iOS 7和8的位置更新 - cordova getting Location Updates for iOS 7 and 8 when the App is Killed/Terminated/Suspended 应用程序终止时iOS 10更新位置 - iOS 10 update location when app is terminated 获取iOS应用在后台甚至被杀的位置 - Getting location for an iOS app when it is in the background and even killed 当应用未运行(已终止/终止)时,如何保持Core Location和Core Bluetooth运行? - How to keep Core Location and Core Bluetooth running when app is not running (killed/ terminated)? 当应用被用户杀死时如何重新启动位置更新? - How to restart the location update when the app is killed by user? 连续的位置更新,即使应用在iOS中终止 - Continious location updates even if app is terminated in iOS 即使使用 carp_background_location 终止应用程序,也将最新位置发送到 Firestore - send latest location to the Firestore even when the app is terminated using carp_background_location 当app处于后台或处于终止(终止)状态时,在iOS应用中执行任务 - Perform task in iOS app when app is in background or in terminated (killed) state
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM