18

我在使用 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>

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

locationManager.requestAlwaysAuthorization()

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

locationManager.requestWhenInUseAuthorization()

在 iOS 11 中,这仅在使用时请求权限,而不是始终开启权限。

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

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

如果有人能阐明我错过/做错了什么,将不胜感激。

如果我删除权限NSLocationAlwaysUsageDescriptioniOS10 和 iOS11 有相同的问题,即不总是请求权限,请注意一些事项。

再澄清一点。我已经实现了 didChangeAuthorization 委托函数,当用户允许调用警报权限时调用它,requestWhenInUseAuthorization() 但是当我requestWhenInUseAuthorization()在位置管理器上调用函数时,未调用委托方法,就像它从未接收到该调用并且没有向用户。

4

4 回答 4

11

我通过创建一个仅请求权限的快速独立应用程序解决了这个问题,我收到了一个错误日志,指出我使用的密钥是错误的。

我有NSLocationAlwaysAndWhenInUsageDescription而不是NSLocationAlwaysAndWhenInUseUsageDescription奇怪,因为从文档中它指出NSLocationAlwaysAndWhenInUsageDescription应该使用。切换到包含正确的密钥修复问题,现在权限可以按预期在 iOS 11 和 10 中工作。

感谢所有的帮助。

于 2017-09-25T23:31:22.567 回答
8

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

<key>NSLocationUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

现在在您的 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.")
        }
    }
于 2017-09-25T07:08:57.717 回答
1

对于这两种情况,客户和员工,您首先需要致电locationManager.requestWhenInUseAuthorization()

然后,只有当他们是员工时,才添加调用 locationManager.requestAlwaysAuthorization()

请参阅https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization

概述 要为位置服务配置始终授权,请执行以下操作: 将 NSLocationWhenInUseUsageDescription 键和 NSLocationAlwaysAndWhenInUsageDescription 键添加到您的 Info.plist 文件中。(Xcode 在 Info.plist 编辑器中将这些键显示为“Privacy - Location When In Use Usage Description”和“Privacy - Location Always and When In Use Usage Description”。)如果您的应用支持 iOS 10 及更早版本,请添加 NSLocationAlwaysUsageDescription 键到您的 Info.plist 文件。(Xcode 在 Info.plist 编辑器中将此键显示为“Privacy - Location Always Usage Description”。)创建并配置您的 CLLocationManager 对象。最初调用 requestWhenInUseAuthorization() 以启用应用的基本位置支持。

于 2017-09-25T06:12:03.220 回答
0

** Swift 5.1 中的最新工作代码:**

Plist:添加条目

<key>NSLocationWhenInUseUsageDescription</key>
  <string>Needs Location when in use</string>


import UIKit
import CoreLocation

class ViewController: UIViewController {
    var locationManager: CLLocationManager?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        locationManager = CLLocationManager()
        
        //Make sure to set the delegate, to get the call back when the user taps Allow option
        locationManager?.delegate = self
    }
}

extension ViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .notDetermined:
            print("not determined - hence ask for Permission")
            manager.requestWhenInUseAuthorization()
        case .restricted, .denied:
            print("permission denied")
        case .authorizedAlways, .authorizedWhenInUse:
            print("Apple delegate gives the call back here once user taps Allow option, Make sure delegate is set to self")
        }
    }
}
于 2021-09-07T09:47:17.693 回答