gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:48:18 29 4
gpt4 key购买 nike

我正在编写一个需要“始终定位”的应用程序,并且我决定使用单例来保持跟踪事件,因为即使在后台,我大多数时间也需要定位服务。

当我在 iPhone 上运行该应用程序时,控制台显示位置服务处于“使用时”模式,并且我的协议(protocol)无法从 LocationManager 获取位置更新。

我的 Singleton 有什么问题(我是 Swift 新手,请清楚地回答。使用 Singleton 进行位置服务是个好主意吗?

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

然后我的 View 符合我的协议(protocol):

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。
  • 启用位置更新的后台模式。
  • 当应用移至后台时切换到重要位置更新。

关于ios - 定位服务作为 Swift 中的单例,卡在 "When In Use"上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55933283/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com