如何防止WKWebView重复请求访问位置的权限? [英] How to prevent WKWebView to repeatedly ask for permission to access location?
问题描述
在我的应用中有一个 WKWebView
,当我开始浏览www.google.com或任何其他需要位置服务的网站时,会弹出一个窗口,要求即使我已经接受分享我的位置的权限,也可以访问设备的位置。
我管理这个位置的唯一东西是我添加了 NSLocationWhenInUseUsageDescription
属性在我的 info.plist
中。
I在网上找不到任何答案,所以任何想法都会得到真正的赞赏。
结果很难,但可能去做。你必须注入拦截请求到 navigator.geolocation
的JavaScript代码并将它们传送到你的应用程序,然后用 CLLocationManager $ c $获取位置然后将位置注入到JavaScript中。
以下是简单方案:
- $ b $将
-
使用
WKUserContentController.add(_:name: code>将脚本消息处理程序添加到
WKWebView
中。注入JavaScript应该调用您的处理程序,如下所示:
window.webkit.messageHandlers.locationHandler.postMessage('getCurrentPosition');
-
当一个网页要求一个位置时,这个方法会触发
userContentController(_:didReceive:)
这样你的应用程序就会知道网页正在请求位置。像往常一样在CLLocationManager
的帮助下找到你的位置。 现在是时候注入位置了使用
WKUserScript
添加到 WKWebView
配置中,该配置将覆盖 navigator方法.geolocation
。注入JavaScript应该看起来像这样: navigator.geolocation.getCurrentPosition = function(success,error,options){...} ;
navigator.geolocation.watchPosition =函数(成功,错误,选项){...};
navigator.geolocation.clearWatch = function(id){...};
webView.evaluateJavaScript(didUpdateLocation({coords:{latitude:55.0,longitude:0.0},timestamp:1494481126215.0}))
来请求JavaScript。 当然,你的注入JavaScript应该有
didUpdateLocation
函数准备启动已保存的成功hanlder。 相当长的算法,但效果很好!
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.
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.
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.
Here is the brief scheme:
Add
WKUserScript
to yourWKWebView
configuration which overrides methods ofnavigator.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) { ... };
With
WKUserContentController.add(_:name:)
add script message handler to yourWKWebView
. Injected JavaScript should call your handler, like this:window.webkit.messageHandlers.locationHandler.postMessage('getCurrentPosition');
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 ofCLLocationManager
as usual.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 havedidUpdateLocation
function ready to launch saved success hanlder.
Quite a long algorithm, but it works!
这篇关于如何防止WKWebView重复请求访问位置的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!