【问题标题】:Objective C - CLLocationManager find out when "Allow" or "Don't allow" is clickedObjective C - CLLocationManager 找出何时单击“允许”或“不允许”
【发布时间】:2011-09-28 07:21:11
【问题描述】:

在执行 CLLocationManager 时,当用户单击“允许”或“不允许”提示请求使用位置时,是否会调用委托方法?

我试过了,但在用户“允许”或“不允许”之后不会调用它。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

另外,是否有一个变量可以告诉我用户选择了什么?

我尝试了以下方法,但总是返回 true。

locationManager.locationServicesEnabled

谢谢你,
三通

【问题讨论】:

    标签: objective-c cllocationmanager


    【解决方案1】:

    有一个委托方法

    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
        if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
            // user allowed
        }
    
    }
    

    【讨论】:

    • 谢谢彼得。有了这个,一种方法可以添加 [[NSNotificationCenter defaultCenter] postNotificationName:kLocationManagerUserDidEnableMonitoring object:nil];而在要通知的Object中,[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleLocationEnabled) name:kLocationManagerUserDidEnableMonitoring object:nil];
    【解决方案2】:

    [CLLocationManager locationServicesEnabled] 只会告诉您设备上是否启用了定位服务。

    [CLLocationManager authorizationStatus] 返回您正在查找的实际状态。

    【讨论】:

      【解决方案3】:

      你必须实现didFailWithError: 方法:

      - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
      
          if ([error domain] == kCLErrorDomain) {
      
              // We handle CoreLocation-related errors here
              switch ([error code]) {
              // "Don't Allow" on two successive app launches is the same as saying "never allow". The user
              // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
              case kCLErrorDenied:
      
              case kCLErrorLocationUnknown:
      
              default:
                  break;
              }
      
          } else {
          // We handle all non-CoreLocation errors here
          }
      }
      

      编辑:查看 CLLocationManager 的参考资料,我发现:

      + (CLAuthorizationStatus)authorizationStatus
      

      返回值 指示应用程序是否被授权的值 使用定位服务。

      讨论给定应用程序的授权状态被管理 由系统和几个因素决定。应用程序必须是 明确授权用户使用位置服务,并且 当前必须为系统启用定位服务本身。 当您的申请时,此授权会自动发生 首次尝试使用位置服务。

      【讨论】:

      • 谢谢恩德。但是,当您单击按钮时,似乎不会调用此方法?看起来它仅在尝试查找 GPS 失败时才被调用。您还知道一个成员变量可以判断用户是允许还是不允许?
      • 请注意,自 iOS 8 以来,此声明不再正确:“此授权在您的应用程序首次尝试使用位置服务时自动进行。”在调用 CLLocationManager 上的 startUpdatingLocation() 之前,您需要显式调用 requestWhenInUseAuthorization()。并确保您在 Info.plist 中有 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 键
      【解决方案4】:

      locationManager.locationServicesEnabled 表示位置服务是否可用,但不一定表示您的应用允许它们。

      如果你需要找出某个时间点的状态,使用CLLocationManager.authorizationStatus(),或者实现

      - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

      请注意,自 iOS 8 起,当您的应用程序首次尝试使用定位服务时,授权请求不会自动发生。在CLLocationManager 实例上调用startUpdatingLocation() 之前,您需要显式调用requestWhenInUseAuthorization()

      并确保您在 Info.plist 中有 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 密钥,具体取决于您所追求的授权类型。如果缺少这些,则没有错误,没有日志,没有提示,没有任何东西可以为您指明正确的方向:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多