位置权限问题 iOS 11 和 iOS 10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46398410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:32:01  来源:igfitidea点击:

Location permission issue iOS 11 and iOS 10

iosswiftios11core-locationios-permissions

提问by Ben Avery

I am having an issue when requesting location permissions from user when I use iOS11 my info.plist contains

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

I have two maps one for customer and another for employees. For employees I need to know their location even if the app is not running or is backgrounded (they are able to turn it off when signing out) and request permission using

我有两张地图,一张供客户使用,另一张供员工使用。对于员工,即使应用程序未运行或处于后台状态(他们可以在退出时将其关闭),我也需要知道他们的位置,并使用

locationManager.requestAlwaysAuthorization()

locationManager.requestAlwaysAuthorization()

For customer i only need locations while the app is in use and request the permission using

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

locationManager.requestWhenInUseAuthorization()

locationManager.requestWhenInUseAuthorization()

In iOS 11 this only requests permission when in usage and never the always on permission.

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

In iOS 10 it has the correct behaviour.

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

The behaviour I want is as follows: When they are a customer (not signed in) it only asks for when in use permission. If they sign in (employee) it request location even when not in use.

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

If anyone can shed some light on what I am missing / done wrong it would be much appreciated.

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

Something to note if i remove the permission NSLocationAlwaysUsageDescriptioniOS10 and iOS11 have the same issue of not requesting always permission.

需要注意的是,如果我删除权限NSLocationAlwaysUsageDescriptioniOS10 和 iOS11 有相同的问题,即不请求始终权限。

A bit more clarification. I have implemented didChangeAuthorization delegate function and it gets called when a users allow the permission from alert from calling requestWhenInUseAuthorization()however when I call requestWhenInUseAuthorization()function on location manager the delegate method is not called it is like it's never receiving that call and no alert dialog is shown to the user.

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

采纳答案by Ben Avery

I figured out the issue by creating a quick stand alone app that only asked for permissions, I was given an error log that stated the keys I was using were wrong.

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

I had NSLocationAlwaysAndWhenInUsageDescriptioninstead of NSLocationAlwaysAndWhenInUseUsageDescriptionwhich is odd because from the docsit states that NSLocationAlwaysAndWhenInUsageDescriptionshould be used. Switching to include the correct key fixed issue and now permissions works as expected for iOS 11 and 10.

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

Thanks for all the help.

感谢所有的帮助。

回答by Akhil Nair

In your info.plist file add this:

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

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

Now in your swift file, don't forget to add delegate: CLLocationManagerDelegate

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

In your viewDiDLoad(), add this:

在您的 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.")
        }
    }

回答by Roee84

For both cases, customers and employees, you first need to call locationManager.requestWhenInUseAuthorization()

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

Then, only if they are employees, add a call to locationManager.requestAlwaysAuthorization()

然后,仅当他们是员工时,才添加呼叫 locationManager.requestAlwaysAuthorization()

See https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/request_always_authorization

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

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.

概述 要为定位服务配置始终授权,请执行以下操作: 将 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() 以启用您的应用程序的基本位置支持。