How to prevent WKWebView to repeatedly ask for permission to access location?(如何防止 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.
以下是简要方案:
将
WKUserScript
添加到您的WKWebView
配置中,它会覆盖navigator.geolocation
的方法.注入的 JavaScript 应该如下所示:
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) { ... };
使用 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 重复请求访问位置的权限?
基础教程推荐
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01