我正在记录我的 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)
我认为我的日志记录格式有问题,所以我也尝试放置一个断点并打印它,但它记录的数字仍然是同样巨大的数字。我也调查了this有一个公平解释的问题,那就是如果你的应用程序在前台,那么时间就会那么长。但我仍然看到这个数字,即使我将应用程序移至后台已有 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 秒后剩余的实际后台时间。
In iOS, regions associated with your app are tracked at all times, including when the app isn’t running. If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event. When necessary, an app can request more background execution time using the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class.
关于ios - 为什么即使应用程序已移至后台,日志记录 backgroundTimeRemaining 也会显示错误/大数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45314183/