【问题标题】:When accessing the settings to enable access to a feature of device, why should I use访问设置以启用对设备功能的访问时,我为什么要使用
【发布时间】:2017-05-17 23:43:15
【问题描述】:

如果之前访问被拒绝,我对于访问设置以启用设备功能的最佳方式有点困惑。我已经看到了两种方法,其中一种似乎正在使用线程。但是我还没有找到一个明确的解释为什么?对此有任何见解。谢谢。

两者都有效,或者下面的 sn-p 作为警报控制器的操作:

alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: {(_ action: UIAlertAction) -> Void in
            //open settings to allow access to geo
            guard let stngUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }
            if UIApplication.shared.canOpenURL(stngUrl) {
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(stngUrl, completionHandler: { (success) in
                    })
                } else {
                    if UIApplication.shared.canOpenURL(stngUrl) {
                        UIApplication.shared.openURL(stngUrl)
                    }
                }
            }                
        }))
        present(alert, animated: true, completion: nil)

或使用 DispatchQueue 像下面的 sn-p?

 alert.addAction(UIAlertAction(title: "Settings", style: .default, handler: {(_ action: UIAlertAction) -> Void in            
        DispatchQueue.main.async(execute: {() -> Void in
            UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
        })
    }))
    present(alert, animated: true, completion: nil)

【问题讨论】:

    标签: ios swift permissions privacy


    【解决方案1】:

    没有理由在UIAlertAction 的处理程序中使用DispatchQueue.main,因为处理程序将始终在主线程上调用。所以使用DispatchQueue.main是多余的。

    【讨论】:

    • 你好,谢谢你的回复,你说的很有趣,所以你说下面这样的话是不必要的吗? stackoverflow.com/a/37420335/5601401
    • 那里不应该需要它,不。
    • 另外,在这里我看到类似的东西,这让我感到困惑,如果 UIAlertAction 的处理程序在主线程上,为什么需要这样的东西:stackoverflow.com/a/31086823/5601401
    • 你具体指的是什么?
    • 对不起,我本来可以更清楚,在第一个链接中他使用 NSOperationQueue.mainQueue().addOperationWithBlock 并在第二个链接中使用 dispatch_async(dispatch_get_main_queue() 现在我不确定为什么需要或者这些链接中的解释并没有真正的帮助。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多