【问题标题】:How to get the exact authorization status of location service?如何获取位置服务的准确授权状态?
【发布时间】:2015-02-10 18:14:02
【问题描述】:

如果用户已明确拒绝此应用程序的授权,或者在设置中禁用了定位服务,它将返回拒绝状态。我怎么知道它的确切原因?

【问题讨论】:

    标签: ios objective-c swift cllocationmanager


    【解决方案1】:

    Swift 5.0 iOS 14.0

    既然'authorizationStatus()' was deprecated in iOS 14.0你应该直接使用实例。

    authorizationStatus 现在是CLLocationManager 的属性。

    您可以使用以下示例:

    import CoreLocation
    
    class LocationManager {
        private let locationManager = CLLocationManager()
        
        var locationStatus: CLAuthorizationStatus {
            locationManager.authorizationStatus
        }
    }
    

    也看看这个问题:AuthorizationStatus for CLLocationManager is deprecated on iOS 14

    【讨论】:

      【解决方案2】:

      斯威夫特:

      我已经制作了这两个函数来检查每个案例

      如果用户明确拒绝对您的应用授权,您可以这样检查,

      class func isLocationDisableForMyAppOnly() -> Bool {
          if CLLocationManager.locationServicesEnabled() && CLLocationManager.authorizationStatus() == .denied {
              return true
          }
          return false
      }
      

      如果在“设置”中禁用了定位服务,

      class func userLocationAvailable() -> Bool {
          return CLLocationManager.locationServicesEnabled()
      }
      

      我就是这样用的,

      if UserLocation.userLocationAvailable() {
              //.... Get location.
      } else {
          if UserLocation.isLocationDisableForMyAppOnly() {
              //... Location not available. Denied accessing location.
          } else {
              //... Location not available. Enable location services from settings.
          }
      }
      

      附注UserLocation 是一个获取用户位置的自定义类。

      【讨论】:

        【解决方案3】:

        斯威夫特

        用途:

        CLLocationManager.authorizationStatus()
        

        获取授权状态。这是一个CLAuthorizationStatus,你可以像这样打开不同的状态:

        let status = CLLocationManager.authorizationStatus()
        switch status {
            case .authorizedAlways:
                <#code#>
            case .authorizedWhenInUse:
                <#code#>
            case .denied:
                <#code#>
            case .notDetermined:
                <#code#>
            case .restricted:
                <#code#>
        }
        

        【讨论】:

        • 我如何知道用户选择了什么? AKA 我如何知道何时用户从“允许...访问您的位置”模式中选择了某些内容?
        【解决方案4】:

        我已经制作了这两个函数来检查每个案例


        如果用户明确拒绝对您的应用授权,您可以这样检查,

        + (BOOL) isLocationDisableForMyAppOnly
        {
            if([CLLocationManager locationServicesEnabled] &&
               [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)
            {
                return YES;
            }
        
            return NO;
        }
        

        如果在“设置”中禁用了定位服务,

        + (BOOL) userLocationAvailable {
            return [CLLocationManager locationServicesEnabled];
        }
        

        我就是这样用的,

        if([UserLocation userLocationAvailable]) {
            //.... Get location.
        }
        else
        {
            if([UserLocation isLocationDisableForMyAppOnly]) {
                //... Location not available. Denied accessing location.
            }
            else{
                //... Location not available. Enable location services from settings.
            }
        }
        

        附注UserLocation 是一个获取用户位置的自定义类。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-24
          • 1970-01-01
          • 1970-01-01
          • 2021-01-29
          • 1970-01-01
          • 2020-03-25
          相关资源
          最近更新 更多