我在后台使用地理定位跟踪,在应用程序处于后台几分钟后,locationManager.stopUpdateLocations() 调用停止工作 - 顶部的蓝条没有被删除,应用程序继续使用地理定位服务(即使启动地理定位后,您只需将应用程序最小化一次)。如果您调用 locationManager.stopUpdateLocations() 时没有将应用程序最小化到后台(或者如果您将应用程序最小化了很短的时间 1-2 分钟),那么服务将关闭并且顶部的蓝条消失。谁面临这个,帮助,我已经花了几天时间 - 都无济于事。禁用所有逻辑,唯一剩下的就是:启动地理定位服务(locationManager.startUpdateLocations)并尝试停止(locationManager.stopUpdateLocations)。
这是带有地理位置的类代码:
import CoreLocation
final class MyLocationService: NSObject, CLLocationManagerDelegate {
private let locationManager = CLLocationManager()
func sendCoord() {
print("Send you coordinates")
}
func requestPermission() {
locationManager.requestWhenInUseAuthorization()
}
func start() {
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.allowsBackgroundLocationUpdates = true
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 300
locationManager.startUpdatingLocation()
sendCoord()
}
func stop() {
locationManager.pausesLocationUpdatesAutomatically = true
locationManager.stopUpdatingLocation()
locationManager.allowsBackgroundLocationUpdates = false
locationManager.delegate = nil
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
sendCoord()
}
}
这是 ViewController 类代码:
import UIKit
import Foundation
import CoreLocation
class ViewController: UIViewController {
// *** Internal variables *** //
private let locationService = MyLocationService()
var needPermissionForLocationServices = false
@IBOutlet weak var toolBar: UIToolbar!
@IBOutlet weak var startLocationToolBarButton: UIBarButtonItem!
@IBAction func startLocationToolBarButtonAction(_ sender: UIBarButtonItem) {
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "Error!", message: "Location services is off", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Go to settings?", style: UIAlertAction.Style.default, handler: { (alert: UIAlertAction!) in
print("")
UIApplication.shared.open(NSURL(string:UIApplication.openSettingsURLString)! as URL)
}))
self.present(alert, animated: true, completion: nil)
})
case .authorizedAlways, .authorizedWhenInUse:
locationService.start()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.tableView.reloadData()
}
if action.lRefresh {
refreshAfterAction(action: action)
}
}
} else {
DispatchQueue.main.async(execute: {
let alert = UIAlertController(title: "Error!", message: "Location services is off", preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "Go to settings?", style: UIAlertAction.Style.default, handler: { (alert: UIAlertAction!) in
print("")
UIApplication.shared.open(NSURL(string:UIApplication.openSettingsURLString)! as URL)
}))
self.present(alert, animated: true, completion: nil)
})
locationService.requestPermission()
}
}
@IBOutlet weak var stopLocationToolBarButton: UIBarButtonItem!
@IBAction func stopLocationToolBarButtonAction(_ sender: UIBarButtonItem) {
locationService.stop()
}
// *** View Controller Functions *** //
deinit {
print("Deinit: VC NO MEMORY LEAKS")
}
override func viewDidLoad() {
super.viewDidLoad()
locationService.requestPermission()
}
override func viewDidDisappear(_ animated: Bool) {
}
override func viewWillAppear(_ animated: Bool) {
view.backgroundColor = UIColor.white
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}