如何防止 WKWebView 重复请求访问位置的权限?

How to prevent WKWebView to repeatedly ask for permission to access location?(如何防止 WKWebView 重复请求访问位置的权限?)

本文介绍了如何防止 WKWebView 重复请求访问位置的权限?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个 WKWebView,当我开始浏览 www.google.com 或任何其他需要定位服务的网站时,会出现一个弹出窗口,询问是否允许访问该位置即使我已经同意分享我的位置,也可以使用设备.

I have a WKWebView in my app and when I start browsing www.google.com or any other website that requires location service, a pop up window appears, asking for the permission to access the location of the device even if I have already accepted to share my location.

我为管理此位置信息所做的唯一一件事就是在我的 info.plist 中添加了 NSLocationWhenInUseUsageDescription 属性.

The only thing I did to manage this location stuff is that I added the NSLocationWhenInUseUsageDescription attribute in my info.plist.

我在网上找不到任何答案,所以任何想法都将不胜感激.

I couldn't find any answers on the web so any idea would be really appreciated.

推荐答案

事实证明这很困难,但可以做到.您必须注入 JavaScript 代码来拦截对 navigator.geolocation 的请求并将它们传输到您的应用程序,然后使用 CLLocationManager 获取位置,然后将位置注入回 JavaScript.

Turns out it's quite hard, but possible to do. You have to inject JavaScript code which intercepts requests to navigator.geolocation and transfer them to your app, then get the location with CLLocationManager, then inject location back to the JavaScript.

以下是简要方案:

  1. WKUserScript 添加到您的 WKWebView 配置中,它会覆盖 navigator.geolocation 的方法.注入的 JavaScript 应该如下所示:

  1. Add WKUserScript to your WKWebView configuration which overrides methods of navigator.geolocation. Injected JavaScript should look like this:

navigator.geolocation.getCurrentPosition = function(success, error, options) { ... };
navigator.geolocation.watchPosition = function(success, error, options) { ... };
navigator.geolocation.clearWatch = function(id) { ... };

  • 使用 WKUserContentController.add(_:name:) 将脚本消息处理程序添加到您的 WKWebView.注入的 JavaScript 应该调用您的处理程序,如下所示:

  • With WKUserContentController.add(_:name:) add script message handler to your WKWebView. Injected JavaScript should call your handler, like this:

    window.webkit.messageHandlers.locationHandler.postMessage('getCurrentPosition');
    

  • 当网页请求位置时,此方法将触发 userContentController(_:didReceive:),以便您的应用知道网页正在请求位置.像往常一样在 CLLocationManager 的帮助下找到您的位置.

  • When a web page will request a location, this method will fire userContentController(_:didReceive:) so your app would know web page is requesting location. Find your location with the help of CLLocationManager as usual.

    现在是时候使用 webView.evaluateJavaScript("didUpdateLocation({coords: {latitude:55.0, longitude:0.0}, timestamp: 1494481126215.0})").当然,你注入的 JavaScript 应该有 didUpdateLocation 函数准备好启动保存的成功处理程序.

    Now it's time to inject the location back to the requesting JavaScript with webView.evaluateJavaScript("didUpdateLocation({coords: {latitude:55.0, longitude:0.0}, timestamp: 1494481126215.0})"). Of course your injected JavaScript should have didUpdateLocation function ready to launch saved success handler.

    相当长的算法,但它有效!

    Quite a long algorithm, but it works!

    这篇关于如何防止 WKWebView 重复请求访问位置的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

  • 本文标题为:如何防止 WKWebView 重复请求访问位置的权限?

    基础教程推荐