在后台永远运行Swift 2.0应用程序以更新服务器的位置

问题描述 投票:11回答:2

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

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

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

注意:我在plist toApp寄存器中有必要的后台模式来进行位置更新。

非常感谢任何工作代码或链接。

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 iphone swift background core-location
2个回答
17
投票

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

无论您的计时器是否正在运行,iOS都会在几分钟后终止您的应用程序。虽然有一种解决方法,但是在写一个离子应用程序时我必须做类似的事情,这样你就可以查看这个here的代码,该链接有一个快速类来管理iOs中的周期性位置更新。

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

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

希望这可以帮助。

更新

在Swift 2中,您还需要:

self.locationManager.allowsBackgroundLocationUpdates = true

1
投票

您可以使用background location tracking库,使用示例:

    var backgroundLocationManager = BackgroundLocationManager()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

     backgroundLocationManager.startBackground() { result in
            if case let .Success(location) = result {
                LocationLogger().writeLocationToFile(location: location)
            }
    }

    return true
}

应用程序被终止或暂停时它正在工作。

© www.soinside.com 2019 - 2024. All rights reserved.