CLLocationManager startUpdatingLocation()在后台

时间:2016-02-18 15:11:40

标签: ios background location pebble-watch

我有一个特定的用例,我希望能够在应用程序已经在后台时启动位置更新。具体来说,我希望能够按下Pebble watchapp上的按钮,这会导致我的同伴iOS应用程序开始更新位置。

只有当iOS应用程序位于前台或刚刚在过去几秒内输入背景(如5?)时,我才能成功从Pebble中启动位置更新。在应用程序在后台运行几秒钟后,我无法再启动更新。我知道更新还没有开始,因为backgroundTimeRemaining开始倒数到0(如果更新确实开始,它将保持最大值)。我还在顶部显示了蓝色条,显示位置更新时(" _____是使用您的位置")并且在这个奇怪的~5秒阈值过去后也无法显示。

我已经查看了几乎所有关于SO的相关问题,并且我已经基本上尝试了所有建议的内容,例如在开始位置更新之前启动后台任务。我也尝试在启动后台任务后的几秒钟内将位置更新的开始延迟,但这也不起作用。

奇怪的是,我还有一个Apple Watch应用程序与Pebble应用程序做同样的事情,Apple Watch能够始终启动位置更新。也许Apple Watch可以在不拆卸iOS应用程序的情况下做些特别的事情吗?

这是在iOS 9上。

感谢任何帮助,我会尝试任何建议。

更新:我整理了一个演示此问题的快速示例应用。 代码发布在下面,或者您可以从GitHub下载项目: https://github.com/JessicaYeh/BackgroundLocationTest

如果您尝试一下,可以看到将延迟设置为2秒有效,但是当延迟时间更长(例如10秒)时,更新不会开始。

//
//  AppDelegate.swift
//  BackgroundLocationTest
//
//  Created by Jessica Yeh on 2/18/16.
//  Copyright © 2016 Yeh. All rights reserved.
//

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    let locationManager = CLLocationManager()
    var backgroundTask: UIBackgroundTaskIdentifier = UIBackgroundTaskInvalid
    var timer: NSTimer?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        debugPrint(__FUNCTION__)

        // Setup location manager
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.allowsBackgroundLocationUpdates = true

        return true
    }

    @objc func checkBackgroundTimeRemaining() {
        debugPrint(UIApplication.sharedApplication().backgroundTimeRemaining)
    }

    @objc func endBackgroundTask() {
        if self.backgroundTask != UIBackgroundTaskInvalid {
            UIApplication.sharedApplication().endBackgroundTask(self.backgroundTask)
            self.backgroundTask = UIBackgroundTaskInvalid
        }
    }

    func applicationWillResignActive(application: UIApplication) {
        debugPrint(__FUNCTION__)
    }

    func applicationDidEnterBackground(application: UIApplication) {
        debugPrint(__FUNCTION__)

        // Cancel old timer and background task
        timer?.invalidate()
        endBackgroundTask()

        // Start a new background task
        backgroundTask = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler {
            self.endBackgroundTask()
        }

        // Start updating location
//        let delaySec = 2.0 // successfully starts location update
        let delaySec = 10.0 // doesn't start location update
        let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delaySec * Double(NSEC_PER_SEC)))
        dispatch_after(delayTime, dispatch_get_main_queue(), {
            debugPrint("Attempting to start updating location")
            self.locationManager.startUpdatingLocation()
        })

        // Keep checking every 3 sec how much background time app has left
        timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("checkBackgroundTimeRemaining"), userInfo: nil, repeats: true)
    }

    func applicationWillEnterForeground(application: UIApplication) {
        debugPrint(__FUNCTION__)
    }

    func applicationDidBecomeActive(application: UIApplication) {
        debugPrint(__FUNCTION__)
    }

    func applicationWillTerminate(application: UIApplication) {
        debugPrint(__FUNCTION__)
    }
}

extension AppDelegate: CLLocationManagerDelegate {
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        debugPrint(__FUNCTION__)
    }

}

1 个答案:

答案 0 :(得分:1)

好吧,我并不完全是你所尝试的,但要确保你已经开启了目标更新功能(功能 - >背景模式)以及iOS 9.0,添加:

if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {

    self.locationManager.allowsBackgroundLocationUpdates = YES;
}

并更改行:

locationManager.requestWhenInUseAuthorization() 

为:

locationManager.requestAlwaysAuthorization().
祝你好运!