CLLocationManager AuthorizationStatus callback?

后端 未结 6 1829
天命终不由人
天命终不由人 2021-02-01 02:38

In my app I have a tab called \"Discover\". The Discover tab will use the users current location to find \"stuff\" near them. Instead of presenting the user with a generic Autho

6条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 03:28

    Similar to tdon's response above, I created a function with a completion block to communicate the status once it is retrieved from the device:

    func retrieveAuthorizationStatus(completion: @escaping (TrackingState) -> ()) {
        let status = CLLocationManager.authorizationStatus()
        switch status {
        case .authorizedWhenInUse:
            completion(.off)
        default:
            completion(.issue)
        }
    }
    

    TrackingState is a separate enum I'm using to manage display within a view controller. You could just as easily pass the authorizationStatus() in the completion block:

    func retrieveAuthorizationStatus(completion: @escaping (CLAuthorizationStatus) -> ()) {
        let status = CLLocationManager.authorizationStatus()
        completion(status)
    }
    

提交回复
热议问题