我在使用ios11时请求用户的位置权限时遇到问题。
<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中,它具有正确的行为。
我想要的行为如下:当他们是客户(未签名)时,仅在使用许可时就要求。如果他们登录(员工),即使不使用时请求位置。
如果有人可以阐明我缺少/做错了什么,这将不胜感激。
如果我删除权限NSLocationAlwaysUsageDescription
IOS10和ios11有相同的问题,那就是不要求始终许可的问题。
更多的澄清。我已经实现了DIDCHANGEAUTHOTHORIZARIZARIZED委托函数,当用户允许呼叫requestWhenInUseAuthorization()
的ARTIST许可时,它会被调用。 但是,当我在位置管理器上调用requestWhenInUseAuthorization()
函数时,委托方法不称为它,就像它从未接听该呼叫,并且没有向用户显示警报对话框。
我通过创建一个只要求权限的快速独立应用来弄清楚了问题,我获得了一个错误日志,说我使用的键是错误的。
我的 NSLocationAlwaysAndWhenInUsageDescription
而不是 NSLocationAlwaysAndWhenInUseUsageDescription
,这很奇怪,因为从文档中指出应使用 NSLocationAlwaysAndWhenInUsageDescription
。切换以包含正确的密钥固定问题,现在的权限可根据iOS 11和10的预期工作。
感谢您的所有帮助。
在您的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.")
}
}
对于案例,客户和员工,您首先需要致电locationManager.requestWhenInUseAuthorization()
那么,只有当他们是员工时,请添加一个电话 locationManager.requestAlwaysAuthorization()
概述以配置始终为位置服务授权,做 以下内容:添加NSLocationWheninUsEdeseDescription键和 nslocationalways和wheninusausagedescription键的info.plist文件。 (Xcode将这些键显示为"隐私 - 使用时位置 描述"one_answers"隐私 - 位置始终以及使用时的位置 描述"在info.plist编辑器中。)如果您的应用程序支持iOS 10 更早的 info.plist文件。(Xcode将此密钥显示为"隐私 - 位置 始终使用描述" info.plist编辑器中。)创建和 配置您的CllocationManager对象。致电 requestWheninUseAuthorization()最初是为了启用您的应用程序的基本 位置支持。仅调用requestAlwaysAuthorization()方法 当您使用需要授权级别的服务时。
** 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")
}
}
}