位置权限问题iOS 11和iOS 10

Ben*_*ery 14 core-location ios swift ios11 ios-permissions

当我使用iOS11我的info.plist包含时,我在向用户请求位置权限时遇到问题

<key>NSLocationWhenInUseUsageDescription</key>
<string>When in use permissions</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>always permissions</string>
<key>NSLocationAlwaysAndWhenInUsageDescription</key>
<string>Always and in usage permissions</string>
Run Code Online (Sandbox Code Playgroud)

我有两张地图供客户使用,另一张供员工使用.对于员工,我需要知道他们的位置,即使应用程序没有运行或后台(他们能够在退出时关闭它)并请求使用权限

locationManager.requestAlwaysAuthorization()

对于客户,我只需要在应用程序正在使用时使用位置并请求使用权限

locationManager.requestWhenInUseAuthorization()

在iOS 11中,这仅在使用时请求权限,而不是始终在权限上.

在iOS 10中,它具有正确的行为.

我想要的行为如下:当他们是客户(未登录)时,它只询问何时使用权限.如果他们登录(员工),即使不使用它也会请求位置.

如果有人能够了解我所遗漏/做错的事情,我将不胜感激.

需要注意的是,如果我删除了NSLocationAlwaysUsageDescriptioniOS10和iOS11 的权限,则同样的问题是不要求总是允许.

多一点澄清.我已经实现了didChangeAuthorization委托函数,当用户允许来自警报的权限时,它会被调用,requestWhenInUseAuthorization() 但是当我requestWhenInUseAuthorization()在位置管理器上调用函数时,没有调用委托方法,就好像它从未接收到该调用并且没有显示警告对话框用户.

Ben*_*ery 9

我通过创建一个只需要权限的快速独立应用程序来解决这个问题,我收到了一个错误日志,说明我使用的密钥是错误的.

我有NSLocationAlwaysAndWhenInUsageDescription而不是NSLocationAlwaysAndWhenInUseUsageDescription奇怪的,因为它说明应该使用的文档NSLocationAlwaysAndWhenInUsageDescription.切换到包含正确的密钥修复问题,现在权限与iOS 11和10的预期一致.

谢谢你的帮助.


Akh*_*air 8

在您的 info.plist 文件中添加以下内容:

<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>
Run Code Online (Sandbox Code Playgroud)

现在在您的 swift 文件中,不要忘记添加委托: CLLocationManagerDelegate

在您的 viewDiDLoad() 中,添加以下内容:

locationManager = CLLocationManager()

locationManager.delegate = self

locationManager.requestWhenInUseAuthorization()

locationManager.desiredAccuracy = kCLLocationAccuracyBest

locationManager.startUpdatingLocation()

locationManager.startMonitoringSignificantLocationChanges()

// Here you can check whether you have allowed the permission or not.

if CLLocationManager.locationServicesEnabled()
    {
        switch(CLLocationManager.authorizationStatus())
        {

        case .authorizedAlways, .authorizedWhenInUse:

            print("Authorize.")

            break

        case .notDetermined:

            print("Not determined.")

            break

        case .restricted:

            print("Restricted.")

            break

        case .denied:

            print("Denied.")
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,这不是 iOS 11 的问题,它需要 NSLocationAlwaysAndWhenInUsageDescription https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization (3认同)
  • 你不需要打破 Swift 开关案例 Akhil (2认同)