为什么记录backgroundTimeRemaining显示错误/大号,即使应用程序已移至后台?

我正在记录我的UIApplication.shared.backgroundTimeRemaining但数量很大。 它几乎是200位数。

这就是我记录它的方式。

  os_log("Lat: %f | Long: %f | RemainingTime: %f ", log: log, type: .default, location.coordinate.latitude, location.coordinate.longitude, UIApplication.shared.backgroundTimeRemaining) 

我认为我的日志记录格式有问题,所以我也尝试放置一个断点并打印它,但它记录的数字仍然是相同的数字。 我也研究了这个有一个公平解释的问题,那就是如果你的应用程序处于前台,那么时间就会那么大。 但是,即使我将应用程序移动到后台已有5分钟,我仍然会看到这个数字。

我为剩余时间获得的样本号是:

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

整个代码:

 import UIKit import CoreLocation import os.log import MapKit class ViewController: UIViewController, CLLocationManagerDelegate{ lazy var locationManager : CLLocationManager = { var manager = CLLocationManager() manager.delegate = self manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation manager.distanceFilter = 1 manager.pausesLocationUpdatesAutomatically = true manager.allowsBackgroundLocationUpdates = true manager.requestAlwaysAuthorization() manager.startUpdatingLocation() return manager }() var lastLocation : CLLocation? var mapView : MKMapView? let log = OSLog(subsystem: "XYZ.LocationAppSubSystem", category: "dumbo") override func viewDidLoad() { super.viewDidLoad() if locationManager.location != nil{ }else { DispatchQueue.main.async { self.locationManager.startUpdatingLocation() } } os_log("view was Loaded", log: log, type: .error) mapView = MKMapView(frame: UIScreen.main.bounds) mapView?.showsUserLocation = true view.addSubview(mapView!) } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { guard let location = locations.last else { return } lastLocation = location // let date = Date().description(with: Locale.current) os_log("Lat: %{public}f | Long: %{private}f | RemainingTime: %{public}f ", log: log, type: .default, location.coordinate.latitude, location.coordinate.longitude, UIApplication.shared.backgroundTimeRemaining) } func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) { os_log("locationManager was paused", log: log) let location = lastLocation os_log("Lat: %{public}f | Long: %{private}f | RemainingTime: %{public}f ", log: log, type: .default, (location?.coordinate.latitude)!, (location?.coordinate.longitude)!, UIApplication.shared.backgroundTimeRemaining) } func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) { os_log("Region was exited", log: log) } func createRegion(location: CLLocation) { let radius = 3.0 let region = CLCircularRegion(center: location.coordinate, radius: radius, identifier: "didPauseLocationUpdates") region.notifyOnExit = true region.notifyOnEntry = false locationManager.startMonitoring(for: region) } func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) { if region?.identifier == "didPauseLocationUpdates"{ os_log("Main Region was Failed to be created", log: log) }else{ os_log("Other regions were checked ", log: log) } } } 

这是因为即使应用程序处于前台甚至关闭,当执行didEnterRegion,didExitRegion(显然也是didUpdateLocations)事件时,应用程序将获得10秒。 在这10秒内,backgroundTimeRemaining值将显示与前景相同。 但是,如果您注册后台任务,backgroundTimeRemaining将显示经过这10秒后剩余的实际后台时间。

在iOS中,系统会始终跟踪与您的应用关联的区域,包括应用未运行的时间。 如果在应用未运行时越过区域边界,则会将该应用重新启动到后台以处理该事件。 同样,如果应用程序在事件发生时被暂停,它会被唤醒,并且会花费很短的时间(大约10秒)来处理事件。 必要时,应用程序可以使用UIApplication类的beginBackgroundTaskWithExpirationHandler:方法请求更多后台执行时间。

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html