Is it possible to turn off location permission in WKWebView?

て烟熏妆下的殇ゞ 提交于 2021-01-27 14:33:44

问题


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:


A couple of notes:

  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.




回答2:


I had this same problem and the above only partially worked. I believe the more correct solution to this problem is to call the error callback with the PERMISSION_DENIED error code so the website can do what it needs to when location access is denied. Here's the code that I ended up using (I only needed the getCurrentPosition override, but you could add the watchPosition one too if you need it):

let removeLocationJS = """
navigator.geolocation.getCurrentPosition = function(success, error, options) {
    error({
        PERMISSION_DENIED: 1,
        code: 1
    });
};
"""

let removeLocation = WKUserScript(source: removeLocationJS, injectionTime: .atDocumentStart, forMainFrameOnly: true)
contentController.addUserScript(removeLocation)

let config = WKWebViewConfiguration()
config.userContentController = contentController

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

Really it seems like WKWebView should take care of this for you if you don't have the entry in your Info.plist, it should just automatically do this (and maybe spit a message into the console to let you know that it did). I'm gonna go file a radar...



来源:https://stackoverflow.com/questions/51576836/is-it-possible-to-turn-off-location-permission-in-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!