【问题标题】:iOS App Location Permission is not Working CurrentlyiOS 应用位置权限当前不起作用
【发布时间】:2016-08-23 16:26:41
【问题描述】:

这是我的通用 ViewController 类,我研究了这个问题iOS app doesn't ask for location permission,我已经有了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription

@IBOutlet weak var ownMapView: MKMapView!

let locationManager : CLLocationManager = CLLocationManager();


override func viewDidLoad() {
    super.viewDidLoad();

    self.locationManager.delegate = self;
    self.locationManager.requestWhenInUseAuthorization();
    self.locationManager.startUpdatingLocation();
}

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    guard status == .AuthorizedWhenInUse else
    {
        print("Location not using");
        return;
    }

    print("Location using.");
    ownMapView.showsUserLocation = true;
    }

即使我添加了 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 这对我也不起作用。它只打印“未使用的位置”

如何正确请求?

【问题讨论】:

  • 第一次运行时,状态应该是“未确定”,你应该得到一个权限弹出窗口。你得到那个弹出窗口了吗?
  • 第一次尝试我选择了不允许选项,然后程序从不询问位置允许请求。
  • 我的意思是应用程序的第一次运行
  • 尝试从设备/模拟器中删除应用并再次运行
  • 我现在正在尝试删除并再次运行它

标签: ios swift mapkit


【解决方案1】:

要正确理解这一点,请查看 CLLAuthorizationStatus 文档: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/#//apple_ref/c/tdef/CLAuthorizationStatus

CLAuthorizationStatus 有几个可能的值:

  • NotDetermined - 用户尚未决定允许/拒绝使用位置
  • 受限 - 应用仅限使用位置
  • Denied - 用户拒绝使用位置信息。
  • AuthorizedAlways - 此应用有权随时启动定位服务。
  • AuthorizedWhenInUse - 应用被授权在前台运行时启动大多数定位服务

您只检查“AuthorizedWhenInUse”状态,但您首先启动它的状态是未确定。

您可以进一步检查如下状态:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        if status  == .NotDetermined
        {
            print ("Loction use not determined")
            return
        }
        if status  == .Denied
        {
            print ("Location determined")
            return
        }
        guard status == .AuthorizedWhenInUse else
        {
            print("Location not using");
            return;
        }

        print("Location using.");
        ownMapView.showsUserLocation = true;
    }

【讨论】:

    【解决方案2】:

    我们是否在 plist 中添加了 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription? .添加这些,它会工作。

    【讨论】:

    • “我已经有 NSLocationAlwaysUsageDescription 和 NSLocationWhenInUseUsageDescription” - 他已经这样做了
    • 是的先生,我已经添加了这两个描述
    猜你喜欢
    • 2019-08-14
    • 2017-01-21
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多