【问题标题】:locationServicesEnabled always return YESlocationServicesEnabled 总是返回 YES
【发布时间】:2011-05-01 08:24:52
【问题描述】:

如果启用了定位服务,我测试了我的设备 (iPod Touch 2G iOS 4.1)

permitted = [locationManager locationServicesEnabled];

无论是否启用了定位服务,我总是得到“是”。我说的是定位服务的通用按钮,而不是应用程序特定的按钮。在装有 iOS 3.2.2 的 iPad 上一切正常。

【问题讨论】:

    标签: iphone objective-c cocoa-touch cllocationmanager


    【解决方案1】:

    记住[locationManager locationServicesEnabled]deprecated since iOS 4.0。 请改用类方法[CLLocationManager locationServicesEnabled]

    The App Specific Button can be retrieved by

    [CLLocationManager authorizationStatus]
    

    【讨论】:

      【解决方案2】:

      当你使用

      [CLLocationManager locationServicesEnabled]
      

      然后您检查是否在整个系统中启用了 locationServices。因此,当您转到“设置”->“位置服务”时,您会看到第一个开关。该方法返回该状态的状态,并且与您的应用无关。

      如果您需要知道您的应用是否可以访问位置服务,请使用@Pascalius 回答。

      【讨论】:

        【解决方案3】:

        当您为位置管理器实现委托时,您应该实现 didFailWithError。如果用户不允许访问位置,您将在那里收到相应的错误

        Apple Documentation States: 如果用户拒绝您的应用程序使用定位服务,此方法将报告kCLErrorDenied 错误。收到此类错误后,您应该停止定位服务。

        【讨论】:

        • 我这样做了,但我说的是用于启用/禁用位置服务的通用按钮(而不是每个应用程序的额外按钮)。此外,locationServicesEnabled 在禁用时不应返回 YES。
        • 很多人对此感到困惑,但请阅读我提供的文档链接,它会返回yes..您必须检查错误。
        • 我能找到的唯一一句话就是你的回答。 locationServicesEnabled 什么时候返回 NO?它在 iPad 上确实如此......我感到困惑的一点是 A Boolean value indicating whether location services are enabled on the device.The user can enable or disable location services from the Settings application by toggling the Location Services switch in General. 所以通用开关和应用程序特定开关没有区别。
        • 还有最有趣的事情 You should check this property before starting location updates to determine whether the user has location services enabled for the current device. If this property contains the value NO and you start location updates anyway, the Core Location framework prompts the user with a confirmation alert asking whether location services should be reenabled. 为什么要检查它是否总是返回 YES,如果我不检查 iOS 会帮我...
        【解决方案4】:

        Swift 3.1 函数返回 -> status:Bool 和 message:String

        func isLocationEnabled() -> (status: Bool, message: String) {
            if CLLocationManager.locationServicesEnabled() {
                switch(CLLocationManager.authorizationStatus()) {
                case .restricted, .denied:
                    return (false,"No access")
                case .authorizedAlways, .authorizedWhenInUse:
                    return(true,"Access")
                }
            } else {
                return(false,"Turn On Location Services to Allow App to Determine Your Location")
            }
        }
        

        【讨论】:

        • 删除 .notDetermined 并将其添加到第二个 case 对我来说效果很好。
        • @WaylanSands 谢谢,我根据您的评论更新了我的答案
        【解决方案5】:
        if(![CLLocationManager locationServicesEnabled] || ([CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedWhenInUse && [CLLocationManager authorizationStatus]!=kCLAuthorizationStatusAuthorizedAlways))
        {
                ; // app doesn't have access to localization to whatever you want
        }
        

        【讨论】:

          【解决方案6】:

          [CLLocationManager locationServicesEnabled]会在用户设置按钮切换到OFF时返回NO,然后我才实现NO。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-04
            • 1970-01-01
            • 1970-01-01
            • 2012-10-19
            • 2019-03-24
            • 1970-01-01
            相关资源
            最近更新 更多