位置权限问题 iOS 11 和 iOS 10
Location permission issue iOS 11 and iOS 10
我在使用 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 中它具有正确的行为。
我想要的行为如下:
当他们是客户(未登录)时,它只要求使用许可。如果他们登录(员工),即使在不使用时也会请求位置。
如果有人能阐明我遗漏/做错了什么,将不胜感激。
如果我删除权限 NSLocationAlwaysUsageDescription
需要注意的事项 iOS10 和 iOS11 有同样的问题,即不总是请求权限。
再澄清一点。
我已经实现了 didChangeAuthorization 委托函数,当用户允许来自警报的权限调用 requestWhenInUseAuthorization()
时,它会被调用
但是,当我在位置管理器上调用 requestWhenInUseAuthorization()
函数时,未调用委托方法,就像它从未收到该调用一样,并且没有向用户显示警告对话框。
对于客户和员工这两种情况,您首先需要致电locationManager.requestWhenInUseAuthorization()
然后,只有当他们是员工时,才添加呼叫
locationManager.requestAlwaysAuthorization()
Overview To configure always authorization for location services, do
the following: Add the NSLocationWhenInUseUsageDescription key and the
NSLocationAlwaysAndWhenInUsageDescription key to your Info.plist file.
(Xcode displays these keys as "Privacy - Location When In Use Usage
Description" and "Privacy - Location Always and When In Use Usage
Description" in the Info.plist editor.) If your app supports iOS 10
and earlier, add the NSLocationAlwaysUsageDescription key to your
Info.plist file. (Xcode displays this key as "Privacy - Location
Always Usage Description" in the Info.plist editor.) Create and
configure your CLLocationManager object. Call the
requestWhenInUseAuthorization() initially to enable your app's basic
location support. Call the requestAlwaysAuthorization() method only
when you use services that require that level of authorization.
在您的 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.")
}
}
我通过创建一个只请求权限的快速独立应用程序解决了这个问题,我得到了一个错误日志,指出我使用的密钥是错误的。
我用的是 NSLocationAlwaysAndWhenInUsageDescription
而不是 NSLocationAlwaysAndWhenInUseUsageDescription
,这很奇怪,因为 docs 表明应该使用 NSLocationAlwaysAndWhenInUsageDescription
。切换为包含正确的密钥已解决问题,现在权限可按预期用于 iOS 11 和 10。
感谢大家的帮助。
** 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")
}
}
}
我在使用 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 中它具有正确的行为。
我想要的行为如下: 当他们是客户(未登录)时,它只要求使用许可。如果他们登录(员工),即使在不使用时也会请求位置。
如果有人能阐明我遗漏/做错了什么,将不胜感激。
如果我删除权限 NSLocationAlwaysUsageDescription
需要注意的事项 iOS10 和 iOS11 有同样的问题,即不总是请求权限。
再澄清一点。
我已经实现了 didChangeAuthorization 委托函数,当用户允许来自警报的权限调用 requestWhenInUseAuthorization()
时,它会被调用
但是,当我在位置管理器上调用 requestWhenInUseAuthorization()
函数时,未调用委托方法,就像它从未收到该调用一样,并且没有向用户显示警告对话框。
对于客户和员工这两种情况,您首先需要致电locationManager.requestWhenInUseAuthorization()
然后,只有当他们是员工时,才添加呼叫
locationManager.requestAlwaysAuthorization()
Overview To configure always authorization for location services, do the following: Add the NSLocationWhenInUseUsageDescription key and the NSLocationAlwaysAndWhenInUsageDescription key to your Info.plist file. (Xcode displays these keys as "Privacy - Location When In Use Usage Description" and "Privacy - Location Always and When In Use Usage Description" in the Info.plist editor.) If your app supports iOS 10 and earlier, add the NSLocationAlwaysUsageDescription key to your Info.plist file. (Xcode displays this key as "Privacy - Location Always Usage Description" in the Info.plist editor.) Create and configure your CLLocationManager object. Call the requestWhenInUseAuthorization() initially to enable your app's basic location support. Call the requestAlwaysAuthorization() method only when you use services that require that level of authorization.
在您的 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.")
}
}
我通过创建一个只请求权限的快速独立应用程序解决了这个问题,我得到了一个错误日志,指出我使用的密钥是错误的。
我用的是 NSLocationAlwaysAndWhenInUsageDescription
而不是 NSLocationAlwaysAndWhenInUseUsageDescription
,这很奇怪,因为 docs 表明应该使用 NSLocationAlwaysAndWhenInUsageDescription
。切换为包含正确的密钥已解决问题,现在权限可按预期用于 iOS 11 和 10。
感谢大家的帮助。
** 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")
}
}
}