ios - 检测何时 requestAlwaysAuthorization 未调用 iOS 14

标签 ios swift core-location

我正在尝试使用多步骤过程请求用户的位置;但是,当调用 requestAlwaysAuthorization 时,如果未执行,我想移动以显示消息。

让我通过展示我的流程来更好地解释

这是我目前的流程

  1. 请求在使用中
    • 用户选择Allow Once -> Go to 2
    • 用户选择Allow While Using App -> Go to 2
    • 用户选择不允许 -> 显示错误信息
  2. 请求始终许可
    • 用户选择 Keep Only While Using -> 显示错误消息(问题 #1:我无法检测到)
    • 用户选择更改为始终允许 -> 显示成功消息

如果从 1 用户选择了 Allow Once 我什至不能调用 requestAlwaysAuthorization 因为调用它会导致什么都没有,甚至 Apple 状态

If the user responded to requestWhenInUseAuthorization() with Allow Once, then Core Location ignores further calls to requestAlwaysAuthorization() due to the temporary authorization.

Source

我的目标是,如果用户从 1 中选择 Allow Once,则应该一起跳过 requestAlwaysAuthorization

如果从 1 Allow Wile Using App 被选中,并且从 requestAlwaysAuthorization Keep Only while using 被选中然后我想知道那个状态。

我知道可以做到,只是不知道怎么做。这是一个应用程序,可以实现我想要实现的目标

enter image description here enter image description here enter image description here

从第一张图片开始,用户选择Allow While Using App,然后选择Keep Only While Using,他们能够检测到它或能够检测到对话出现的事实,并且在用户响应之前,他们不会移动到第三张图片。

另一种情况如下:

enter image description here enter image description here enter image description here

从第一张图片开始,用户选择了Allow Once,从第二张图片开始,无论用户选择什么,他们都将转到第三张图片,而不显示始终允许(请记住,第二张图片不是一个真正的许可,它只是用来欺骗用户的按钮);因此他们能够分辨出对话没有出现,或者用户从上一步中选择了Allow Once(一个或另一个,我不能做任何一个)。

显然我无法记录每个组合并发布为 gif,因此如果您想体验应用名称是 Zenly

因此,最终目标是能够等待用户响应出现的对话,否则就转到下一个屏幕。

这是我所做的,引用自 apple

Core Location leaves the authorization as When In Use. The delegate doesn’t receive any updates.

我想也许当总是请求时,我也应该手动执行委托(delegate),所以我在函数 requestAlwaysLocation() 中添加了这行

self.locationManagerDidChangeAuthorization(self.locationManager)

但是,这显然行不通,因为在 Allow While Using App 从步骤 1Change to Always Allow 的情况下> 是从步骤 2 中选择的,现在委托(delegate)将被调用两次...

然后我想如果我可以从步骤 1 检测到配置 When in Use 我也许可以绕过,但我不知道该怎么做。

这是 LocationManager 的片段

    func requestWhenInUserLocation()  {
        self.locationManager.requestWhenInUseAuthorization()
    }
    
    func requestAlwaysLocation() {
        self.locationManager.requestAlwaysAuthorization()
    }


    func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
        print("authroizatino changed")
//        let accuracyAuthorization = manager.accuracyAuthorization
        let status = manager.authorizationStatus

        // -change function will just change some internal properties and will post a notification so views that subscribe to it can react accordingly.
        
        switch status {
            case CLAuthorizationStatus.authorizedAlways:
                self.change(permission: .Location, to: .Location(.Always))
                break
            case CLAuthorizationStatus.authorizedWhenInUse:
                self.change(permission: .Location, to: .Location(.WhenInUse))
                break
            case CLAuthorizationStatus.denied:
                self.change(permission: .Location, to: .Denied)
                break
            case CLAuthorizationStatus.notDetermined:
                self.change(permission: .Location, to: .NotDetermined)
                break
            case CLAuthorizationStatus.restricted:
                self.change(permission: .Location, to: .Location(.Restricted))
                break
            @unknown default:
                self.change(permission: .Location, to: .Unknown)
                break
        }
    }


我希望我能够清楚地解释我想要什么

最佳答案

感谢@Paulw11 我能够解决它。实际上这很容易。

我不能在这里添加任何实现,因为它实际上取决于一个人的用例,而我使用的是 SwiftUI。

所以我所做的是在按下 Request Always 按钮时我会创建一个 bool 状态并将其设置为 true,我还有一个应用程序事件状态的观察者这是解决这个问题的关键,我还有一个观察者在 bool 状态。

如果 bool 状态发生变化而应用程序的事件状态没有发生变化,那么用户选择了Allow Once;但是,如果状态确实发生了变化,那么我会等待它再次激活。

如果它变为事件状态,我会检查位置权限,如果它仍然是 When in Use 那么用户选择 Keep Only While Using 否则他们选择 Change to Always

所有学分归功于 Paulw11

编辑:如果用户从设置更改位置权限,我也有应用程序从后台转到前台的观察员

注意:如果您将 Combine 与 SwiftUI 一起使用,上述算法可以得到很大改进。我写的大部分没用,你可以订阅权限和应用程序的事件状态。

关于ios - 检测何时 requestAlwaysAuthorization 未调用 iOS 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64761582/

相关文章:

ios - 通过触摸为 UITextView 中的句子添加下划线

swift - 如何计算用户从swift 3.0中的当前位置移动的距离

ios - 核心位置 : efficient way to keep track user's location

ios - 如何在 Swift 中锁定单个 UIView 从旋转到横向?

ios - 在 Swift Playground 实例中运行 CoreLocation

ios - 如何使 UIView 匹配 UILabel 的大小

ios - 如何滑出侧边菜单栏?在 swift

ios - 与 iOS 链接的 Facebook 应用程序

swift - 如何让一个 Sprite 节点围绕另一个节点旋转? (还有一条线)

ios - 隐藏标签栏上的叠加按钮