WKWebView 中的 Google disallowed_useragent

问题描述 投票:0回答:2

我正在尝试在我的应用程序中使用 WebView 页面,但在尝试登录 google 时出现错误。 我知道谷歌需要一个已知的操作系统浏览器才能登录他们的服务,但有一些方法可以绕过他们的 OAuth。在我的代码中应该在哪里实现让谷歌相信WebView是一个浏览器。

    class CycleViewController: UIViewController, WKUIDelegate {

    var window: UIWindow?
    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        webView.backgroundColor = UIColor.clear
        webView.backgroundColor = UIColor(red: 0.0196, green: 0.4, blue: 0.2902, alpha: 1.0)

        webView.isOpaque = false
        let myURL = URL(string: "https://jwelsh19.wixsite.com/countryday")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)

    }
}
ios swift xcode google-oauth wkwebview
2个回答
12
投票

只需在

webConfiguration.applicationNameForUserAgent
中设置您的移动 Safari 代理的正确值即可。尝试使用以下代码。

override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.applicationNameForUserAgent = "Version/8.0.2 Safari/600.2.5"
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }

0
投票

就用这个:

    self.webView.customUserAgent = "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Mobile Safari/537.36"
© www.soinside.com 2019 - 2024. All rights reserved.