即使应用程序被终止或终止,位置也会更新 [英] Location update even when app is killed/terminated

查看:110
本文介绍了即使应用程序被终止或终止,位置也会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即使在应用程序被终止/终止/暂停时,我也试图在所有状态下获取位置更新。我在xcode中启用了后台提取并实现了以下代码(使用参考在所有州应用程序中捕获位置)。但是,当我终止应用程序时,它会在AppDelegate类上给出一条红线。我不明白这里有什么问题。我使用问题解决方案获取iOS应用程序的位置,当它在后台甚至被杀死时,这里,但它不适用于ios 9.Please帮助我或告诉我其他解决方案。

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"). But when i terminate the app it's giving a red line on class 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.

更新代码 -

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

H.打开你会得到你的答案。它对我有用。现在我正在尝试在调用'didUpdateLocations'函数时发出http请求,这样我就可以在应用程序未运行时将用户当前位置存储在服务器中(暂停/终止/终止)。

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).

我不认为苹果允许在应用程序暂停/终止时发出任何http请求。但是如果服务器收到了位置更新的请求,那么我很高兴去那。因为我不需要从服务器回复。需要大量测试....

I don't think apple allowed to make any http request when the app is suspended/terminated. 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....

这篇关于即使应用程序被终止或终止,位置也会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆