定位服务作为 Swift 中的单例,卡在"When In Use"



我正在编程一个需要"始终位置"的应用程序,我决定使用singleton继续跟踪活动,因为我大部分时间都需要位置服务,即使在后台。<<<<<<<<<</p>

当我在iPhone上运行应用程序时,控制台说位置服务处于"使用时"模式,并且我的协议不会从位置manager获取位置更新。

我的单身人士怎么了(我是一个迅速的新手,请在您的答案中清楚。使用单身顿进行位置服务是个好主意吗?

locationservice.swift(更新(

import Foundation
import CoreLocation
protocol LocationServiceDelegate {
    func onLocationUpdate(location: CLLocation)
    func onLocationDidFailWithError(error: Error)
}
class LocationService: NSObject, CLLocationManagerDelegate {
    public static let shared = LocationService()
    var delegate: LocationServiceDelegate?
    var locationManager: CLLocationManager!
    var currentLocation: CLLocation!
    private override init() {
        super.init()
        self.initializeLocationServices()
    }
    func initializeLocationServices() {
        self.locationManager = CLLocationManager()
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.pausesLocationUpdatesAutomatically = false
        self.locationManager.delegate = self
        self.locationManager.startUpdatingLocation()
    }
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
            case .restricted:
                print("Location access was restricted.")
            case .denied:
                print("User denied access to location.")
            case .notDetermined:
                self.locationManager.requestAlwaysAuthorization()
            case .authorizedAlways: fallthrough
            case .authorizedWhenInUse:
                print("User choosed locatiom when app is in use.")
            default:
                print("Unhandled error occured.")
        }
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.currentLocation = locations.last!
        locationChanged(location: currentLocation)
    }
    private func locationChanged(location: CLLocation) {
        guard let delegate = self.delegate else {
            return
        }
        delegate.onLocationUpdate(location: location)
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        self.locationManager.stopUpdatingLocation()
        locationFailed(error: error)
    }
    private func locationFailed(error: Error) {
        guard let delegate = self.delegate else {
            return
        }
        delegate.onLocationDidFailWithError(error: error)
    }
}

然后我初始化单例:

appdelegate.swift

 let locationService = LocationService.shared

然后我的观点符合我的协议:

viewcontroller.swift

 extension ViewController: LocationServiceDelegate {
    func onLocationUpdate(location: CLLocation) {
        print("Current Location : (location)")
    }
    func onLocationDidFailWithError(error: Error) {
        print("Error while trying to update device location : (error)")
    }
}

是的,您可以将Singleton用于目的。您可以通过实施来检查几件事:

  • locationManager.pauesslocationupdatesautopical = false。
  • 启用位置更新的背景模式。
  • 当应用移至背景时,切换到重要位置更新。

是将通知发送给所有ViewControllers以传递Cllocation对象的更好方法,或者更好地符合每个控制器中的协议?

最新更新