在后台永远运行Swift 2.0应用程序以更新服务器的位置 [英] Run a Swift 2.0 app forever in background to update location to server

查看:165
本文介绍了在后台永远运行Swift 2.0应用程序以更新服务器的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了下面的代码,它有一个每分钟调用一次回调函数的计时器。当应用程序进入后台时,我启动了另一个调用相同回调方法的计时器,但后台计时器只能运行三分钟。

I have written the below code that has a timer that calls a callback function every minute. When the app goes to the background I have started another timer that calls the same callback method, but the background timer works for only three minutes.

我知道Apple允许后台运行任务只有三分钟。我的应用程序更像是一个跟踪应用程序,即使应用程序处于后台,也可以每分钟跟踪用户的位置,因此我需要实现此功能。

I understand that Apple allows background tasks for only three minutes. My app is more like a tracking app that tracks the location of the user every minute even when the app is in background, so I need to implement this functionality.

我学到了将使用 beginBackgroundTaskWithExpirationHandler ,但我不知道我的实现是否正确。

I learned that beginBackgroundTaskWithExpirationHandler is to be used but I don't know whether my implementation is correct.

注意:我有plist toApp寄存器中所需的后台模式用于位置更新。

Note: I have Required background modes in plist toApp registers for location updates.

非常感谢任何正在运行的代码或链接。

Any working code or links are much appreciated.

override func viewDidLoad() {
    super.viewDidLoad()

    timeInMinutes = 1 * 60

    self.locationManager.requestAlwaysAuthorization()

    self.locationManager.requestWhenInUseAuthorization()

    if CLLocationManager.locationServicesEnabled() {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.startUpdatingLocation()
    }

    var timer = NSTimer.scheduledTimerWithTimeInterval( timeInMinutes, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    let locValue:CLLocationCoordinate2D = manager.location!.coordinate

    self.latitude = locValue.latitude
    self.longitude = locValue.longitude

    if UIApplication.sharedApplication().applicationState == .Active {

    } else {
        backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
            self.backgroundTimer.invalidate()
            self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval( 60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
        })
    }
}

func updateLocation() {
    txtlocationLabel.text = String(n)
    self.n = self.n+1

    var timeRemaining = UIApplication.sharedApplication().backgroundTimeRemaining

    print(timeRemaining)

    if timeRemaining > 60.0 {
        self.GeoLogLocation(self.latitude,Longitude: self.longitude) {
            results in
            print("View Controller: \(results)")
            self.CheckResult(String(results))
        }
    } else {
        if timeRemaining == 0 {
            UIApplication.sharedApplication().endBackgroundTask(backgroundTaskIdentifier)
        }

        backgroundTaskIdentifier2 = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
            self.backgroundTimer.invalidate()
            self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval( 60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
        })
    }
}


推荐答案

IOS中的定期位置更新有点棘手。有一个讨论这个的好线程,你可以阅读更多此处

Periodic location updates are a bit tricky in IOS.There's a good thread that discusses this, you can read more here

iOS将在几分钟后终止您的应用,无论您的计时器如何正在运行与否。虽然有一种解决方法,但是在写一个离子应用程序时我必须做类似的事情,所以你可以查看这个代码这里,该链接有一个swift类,用于管理iOs中的定期位置更新。

iOS will terminate your app after a few minutes, regardless if your timer is running or not. There is a way around this though, I had to do something similar when writing an ionic app so you can check out the code for this here, that link has a swift class that manages the periodic location updates in iOs.

为了在后台定期获取位置,而不是耗尽设备的电池,你需要发挥位置记录的准确性,降低准确性位置管理器将其所需的准确度设置为kCLLocationAccuracyThreeKilometers,然后,每60秒需要将准确度更改为kCLLocationAccuracyBest,这将使委托获得新的,准确的位置更新,然后将准确性恢复为低。每次收到更新时都需要初始化定时器。

In order to get periodic locations in the background, and not drain the battery of the device, you need to play with the accuracy of the location records, lower the accuracy of the location manager setting its desiredAccuracy to kCLLocationAccuracyThreeKilometers, then, every 60 seconds you need to change the accuracy to kCLLocationAccuracyBest, this will enable the delegate to get a new, accurate location update, then revert the accuracy back to low. The timer needs to be initialized every time an update is received.

还有一种方法可以在应用程序被用户杀死之后在后台唤醒应用程序,使用app delegate让应用程序监听位置的重大变化它被杀了。当用户的位置发生大跳跃时(这可能是大约200毫秒),这将在后台唤醒应用程序。当应用程序唤醒时,停止监视重大更改并像往常一样重新启动位置服务以继续定期更新。

There's also a way to wake up the app in the background after its been killed by the user, use the app delegate to have the app listen for significant changes in location before its killed. This will wake up the app in the background when the user's location makes a big jump (can be around 200ms). When the app wakes up, stop monitoring for significant changes and restart the location services as usual to continue the periodic updates.

希望这会有所帮助。

更新

在Swift 2中,您还需要:

In Swift 2 you'll also need:

self.locationManager.allowsBackgroundLocationUpdates = true

这篇关于在后台永远运行Swift 2.0应用程序以更新服务器的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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