是否可以在 WKWebView 中关闭位置权限? [英] Is it possible to turn off location permission in WKWebView?

查看:23
本文介绍了是否可以在 WKWebView 中关闭位置权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以阻止 WKWebView 显示位置权限提示?(website.com"想使用您当前的位置)我相信它显示是因为该网站包含一个谷歌地图.我对在其他 SO 问题中显示的位置预加载位置不感兴趣.我只是不想在 WKWebView 中使用位置.有没有办法阻止位置权限提示出现?我试过注入以下 javascript 但它不起作用.

I am wondering if it is possible to stop WKWebView from showing the location permission prompt? ("website.com" Would Like To Use Your Current Location) I believe it is showing because the website contains a google map. I am not interested in preloading a location in its place like is shown in other SO questions. I simply don't want to use location in the WKWebView. Is there a way to stop the location permission prompt from appearing? I have tried injecting the following javascript but it doesn't work.

        let contentController = WKUserContentController()

        let scriptSource = "navigator.geolocation.getCurrentPosition = function(success, error, options) { // }; navigator.geolocation.watchPosition = function(success, error, options) { // }; navigator.geolocation.clearWatch = function(id) { // };"
        let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
        contentController.addUserScript(script)

        let config = WKWebViewConfiguration()
        config.userContentController = contentController

        webView = WKWebView(frame: .zero, configuration: config)
        self.view = webView

推荐答案

注意事项:

  1. .atDocumentEnd 表示你的脚本会在页面加载后执行,这可能会导致javascript在你劫持定位功能之前要求定位;您可能会使用 .atDocumentStart
  2. 获得更好的结果
  3. 您在脚本中有 // 序列,这可能会导致脚本的其余部分被解释为注释,从而导致错误的 JS 被注入
  4. 函数的签名似乎与 HTML 5 Geolocation API,虽然考虑到我们在 JS 世界中并且函数体是空的,但它可能没有什么区别.
  1. .atDocumentEnd means your script will execute after the page loads, and this might result in the javascript asking for location running before you hijack the location functions; you might get better results with .atDocumentStart
  2. you have // sequences in the script, which will likely result in the rest of the script being interpreted as comments, leading to incorrect JS being injected
  3. the signature of the functions don't seem to match the HTML 5 Geolocation API, though considering we're in JS world and the function bodies are empty, it might not make a difference.

尝试修复上述问题,这可能会导致您需要的行为.

Try fixing the above issues, this might result in the behaviour you need.

这篇关于是否可以在 WKWebView 中关闭位置权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆