当应用程序处于后台或被杀时,如何定期获取新位置

时间:2017-01-07 18:23:29

标签: ios swift3 xcode8 cllocation

当我的应用处于后台或被杀时,我想每1分钟获取一个新位置。 这是我的代码:

位置服务:

class LocationService: NSObject, CLLocationManagerDelegate {

    static let shared = LocationService()
    var locationManager = CLLocationManager()
    var significatLocationManager = CLLocationManager()
    var currentLocation: CLLocation!
    var timer = Timer()
    var bgTask = UIBackgroundTaskInvalid

    func startUpdatingLocation() {
        locationManager.requestAlwaysAuthorization()
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.distanceFilter = kCLDistanceFilterNone
        if #available(iOS 9.0, *) {
            locationManager.allowsBackgroundLocationUpdates = true
        }
        locationManager.delegate = self
        locationManager.startUpdatingLocation()
    }

    func stopUpdatingLocation() {
        locationManager.stopUpdatingLocation()
        timer.invalidate()
    }

    func restartUpdatingLocation() {
        locationManager.stopMonitoringSignificantLocationChanges()
        timer.invalidate()
        startUpdatingLocation()
    }

    func startMonitoringLocation() {
        locationManager.stopUpdatingLocation()
        locationManager.startMonitoringSignificantLocationChanges()
        timer.invalidate()
    }

    func changeLocationAccuracy(){
        switch locationManager.desiredAccuracy {
        case kCLLocationAccuracyBest:
            locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
            locationManager.distanceFilter = 99999
        case kCLLocationAccuracyThreeKilometers:
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.distanceFilter = kCLDistanceFilterNone
        default: break
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {
            UIApplication.shared.endBackgroundTask(self.bgTask)
            self.bgTask = UIBackgroundTaskInvalid
        })

        currentLocation = locations.last!

        let interval = currentLocation.timestamp.timeIntervalSinceNow
        let accuracy = self.locationManager.desiredAccuracy

        if abs(interval) < 60 && accuracy != kCLLocationAccuracyThreeKilometers {        
            timer = Timer.scheduledTimer(timeInterval: 60, target: self, selector: #selector(LocationService.changeLocationAccuracy), userInfo: nil, repeats: false)
            changeLocationAccuracy()
        }
    } 
}

App代表:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
     if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
          LocationService.shared.restartUpdatingLocation()
     }
}

func applicationDidEnterBackground(_ application: UIApplication) {
     LocationService.shared.restartUpdatingLocation()
}

func applicationWillTerminate(_ application: UIApplication) {
     LocationService.shared.startMonitoringLocation()
}

查看控制器:

override func viewDidLoad() {
    LocationService.shared.startUpdatingLocation()
}

但我的代码无法正常工作,几分钟后,当我的应用处于后台时,我没有获得新位置。当我的应用程序被暂停或杀死时也一样 那有什么解决方案吗?请注意,我使用的是Swift 3&amp; xcode 8.1。

提前感谢

1 个答案:

答案 0 :(得分:2)

  • 如果您的应用已在后台时拨打startUpdatingLocation,那么您只会在几分钟内收到位置更新。

  • 如果您在应用处于前台时呼叫startUpdatingLocation,然后您的应用移至后台,您将无限期地继续接收位置更新。

  • 如果您在前台中呼叫startMonitoringSignificantLocationChanges并启用,则可以在后台呼叫startUpdatingLocation并无限期地接收更新。

  • 如果用户终止您的应用(通过在应用切换器中向上滑动),则在用户重新启动您的应用之前,您将不再接收位置更新。

由于您的代码在移动到仅获取位置更新几分钟的后台时停止并启动位置监控。如果您启动位置监控前台,并在应用移动到后台时不执行任何操作,那么您将继续接收位置更新。