我有一个单独的类,名为LocationService
class LocationService: NSObject, CLLocationManagerDelegate {
private var locationManager = CLLocationManager()
private var locationCallback: ((CLLocation?) -> Void)!
override init(){
super.init()
self.locationManager.delegate = self
}
public func getCurrentLocation (_ completion: @escaping(CLLocation?) -> Void) {
if PermissionManager.hasLocationPermission() && CLLocationManager.locationServicesEnabled() {
self.locationCallback = completion
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
} else {
completion(nil)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("LOCATIONS: ", locations)
locationCallback(locations.first)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("LOCATION ERROR: ", error)
locationCallback(nil)
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
print("LOCATION: ", newLocation)
locationCallback(newLocation)
}
deinit {
print("DEINIT LOCATION SERVICE")
}
}
如果我触发getCurrentLocation
,则没有调用任何位置管理器委托函数,尽管已到达startUpdatingLocation
。
我没有视图控制器,因为它在反应本机插件内。在使用中的位置描述是在info.plist中设置的,从来没有调用deinit函数。此外,还授予了位置许可。
有人知道缺了什么吗?
发布于 2022-02-15 14:33:25
protocol LocationManagerDelegate {
func didAuthorized()
func didUpdateLocation(location: CLLocation?, isFirst: Bool)
}
final class LocationManager: NSObject {
static let shared = LocationManager()
let delegates = MulticastDelegate<LocationManagerDelegate>()
private var locationManager = CLLocationManager()
var currentLocation: CLLocation?
var isStop = false
var denied = false
override init() {
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
}
func request() {
if CLLocationManager.locationServicesEnabled() {
switch CLLocationManager.authorizationStatus() {
case .notDetermined, .restricted, .denied:
self.denied = true
self.locationManager.requestWhenInUseAuthorization()
case .authorizedAlways, .authorizedWhenInUse:
self.isStop = false
self.denied = false
self.locationManager.startUpdatingLocation()
default:
break
}
}
}
func stop() {
self.isStop = true
self.locationManager.stopUpdatingLocation()
}
}
这是扩展
extension LocationManager: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedWhenInUse || status == .authorizedAlways {
self.isStop = false
if self.denied {
self.denied = false
self.locationManager.startUpdatingLocation()
self.locationManager.requestAlwaysAuthorization()
}
self.delegates.invoke(invocation: { $0.didAuthorized() })
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.first, isStop == false else {
return
}
var isFirst = false
if self.currentLocation == nil {
isFirst = true
}
self.currentLocation = location
self.delegates.invoke(invocation: { $0.didUpdateLocation(location: location, isFirst: isFirst) })
}
func checkLocationPermission() -> Bool {
// check the permission status
switch CLLocationManager.authorizationStatus() {
case .authorizedAlways, .authorizedWhenInUse:
return true
default:break
}
return false
}
}
也许您必须使用以下内容: static = LocationManager()
当我想要定位时,我使用: LocationManager.shared.request()
这个对我有用
https://stackoverflow.com/questions/71127881
复制相似问题