首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CLLocationManagerDelegate函数未调用

CLLocationManagerDelegate函数未调用
EN

Stack Overflow用户
提问于 2022-02-15 14:07:53
回答 2查看 233关注 0票数 0

我有一个单独的类,名为LocationService

代码语言:javascript
运行
复制
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函数。此外,还授予了位置许可。

有人知道缺了什么吗?

EN

Stack Overflow用户

发布于 2022-02-15 14:33:25

代码语言:javascript
运行
复制
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()
 }
}

这是扩展

代码语言:javascript
运行
复制
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()

这个对我有用

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71127881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档