首页 > 解决方案 > 应用从后台状态返回后 locationManager.stopUpdateLocations 不起作用

问题描述

我在后台使用地理定位跟踪,当应用程序在后台运行几分钟后,对 locationManager.stopUpdateLocations () 的调用停止工作 - 顶部的蓝条没​​有被删除,应用程序继续使用地理定位服务。如果您调用 locationManager.stopUpdateLocations() 而不将应用程序最小化到后台,则服务将关闭并且顶部的蓝条消失。任何遇到过这种情况的人,请帮忙,已经花了几天时间 - 都无济于事。禁用所有逻辑,剩下的就是:启动地理定位服务(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()
                }
            } 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()
    
    }
}

标签: iosswiftswift4

解决方案


寻找解决方案。您只需为 My Location Service 类创建一个共享实例,而不是在 ViewController 中将此类的实例创建为变量,而是调用相同的单例。原因是我创建了 My Location Service 类的不同实例,并在一个实例中启动了位置,并试图在另一个实例中停止它。

class var sharedInstance: MyLocationService {
    struct Singleton {
        static let instance = MyLocationService()
    }

    return Singleton.instance
}

并调用类方法:

MyLocationService.sharedInstance.start()

推荐阅读