【问题标题】:Location update even when app is killed/terminated即使应用程序被杀死/终止,位置更新
【发布时间】:2016-04-04 08:04:25
【问题描述】:

即使应用程序被终止/终止/暂停,我也在尝试获取所有州的位置更新。我在 xcode 中启用了后台获取并实现了以下代码(使用参考“Capture location in all states app”)。但是当我终止应用程序时,它会在 AppDelegate 类上显示一条红线。我不明白这里有什么问题。我在这里使用问题“Getting location for an iOS app when it is in the background and even killed”的解决方案完成了此操作,但它在 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()
    }


}

【问题讨论】:

  • “它在 AppDelegate 类上给出了一条红线” - 这是最糟糕和最没有帮助的错误描述之一。请在此处发布错误消息。如果您通过 Xcode 运行您的应用程序,如果您强制终止应用程序,则出现“红线”是正常的。
  • 是的,我强行终止了应用程序并显示了这个红线错误,但我没有在终止应用程序时获得位置更新。这里有什么问题??
  • @Cade..此代码不起作用,我正在努力寻找解决方案!!!
  • 很高兴知道哪一行...以及错误...

标签: ios swift location core-location


【解决方案1】:

查看本教程: http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

还有这里的源代码: https://github.com/voyage11/GettingLocationWhenSuspended

希望你能得到答案。它对我有用。现在我正在尝试在调用“didUpdateLocations”函数时发出 http 请求,以便在应用程序未运行时(暂停/终止/终止)将用户当前位置存储在服务器中。

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

【讨论】:

  • 嗨,当应用程序处于终止状态时,您是否发现任何成功的 HTTP 调用?
  • 是的。我做到了。当应用程序状态处于终止状态时。 iOS 控制应用程序何时唤醒并进行位置更新调用。但它确实对我有用。
  • 根据您的博客,您提到位置管理器每 10 分钟更新一次。我知道一个应用程序,即 Life 360​​,它会在用户更改位置后立即更新位置。您知道我们如何实现这一目标吗?
  • @Leena 你可以使用区域监控。
  • @SaqibOmerI 我成功地使用位置管理器在终止状态下获取位置更新,该管理器仅工作了半小时,并且在我的应用程序正在更新位置时发生重大变化。
猜你喜欢
  • 1970-01-01
  • 2022-10-06
  • 2017-07-13
  • 1970-01-01
  • 2015-05-01
  • 2017-03-09
  • 1970-01-01
  • 2016-04-15
相关资源
最近更新 更多