ios - 定位服务作为 Swift 中的单例,卡在“使用时”
问题描述
我正在编写一个需要“始终定位”的应用程序,我决定使用单例来保持跟踪活动,因为我大部分时间都需要定位服务,即使在后台也是如此。
当我在我的 iPhone 上运行应用程序时,控制台说定位服务处于“使用时”模式,并且我的协议没有从 LocationManager 获取位置更新。
我的单例有什么问题(我是 Swift 新手,请在你的答案中明确。使用单例进行定位服务是个好主意吗?
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)")
}
}
解决方案
是的,您可以将单例用于您的目的。您可以通过实施检查几件事:
- locationManager.pausesLocationUpdatesAutomatically = false。
- 为位置更新启用后台模式。
- 当应用程序移至后台时切换到重要的位置更新。
推荐阅读
- kubernetes - Kubernetes:如何配置/实现 HPA 以使用 CPU 进行扩展
- python-3.x - 在 openpyxl 折线图中显示隐藏的行
- python - Tkinter:如何检查条目小部件中的用户输入是否与数据库中的某些信息匹配
- java - Mongo 两个字符串的总和
- wix - Wix 工具集 - 为什么第二个自定义操作不在安装时运行?
- python - I have a problem the random.choice() function as it prints one LETTER in a list of WORDS
- sql - 过滤中的几个条件 - SQL
- .net - 我怎样才能做出正确的 URL 重写条件?
- python - Python反射,方法的getter
- syntax - 如何识别配置文件的语法及其等效的 Markdown 语言标识符?