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
WKUserScriptto yourWKWebViewconfiguration 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 重复请求访问位置的权限?
基础教程推荐
- 我的 UIImageView 的任务 2022-01-01
- 新的@SystemApi 注解是什么意思,和@hide 有什么区别 2022-01-01
- - (CGRect)convertRect:(CGRect)rect toView:(UIView *)view 如何工作 2022-01-01
- 可可/目标C(OSX不是iOS)从子对象访问父对象 2022-01-01
- 在 iOS8 中无法获得正确的键盘高度值 2022-01-01
- 突出显示朗读文本(在 iPhone 的故事书类型应用程序中) 2022-01-01
- Android:STATE_SELECTED不工作 2022-01-01
- 在 appComponent dagger 2 中动态添加测试模块? 2022-01-01
- 如何将多个组件添加到 PickerView? 2022-01-01
- 在 Android 模拟器中激活网络位置提供程序? 2022-01-01
