<tfoot id='uX9yq'></tfoot>

<i id='uX9yq'><tr id='uX9yq'><dt id='uX9yq'><q id='uX9yq'><span id='uX9yq'><b id='uX9yq'><form id='uX9yq'><ins id='uX9yq'></ins><ul id='uX9yq'></ul><sub id='uX9yq'></sub></form><legend id='uX9yq'></legend><bdo id='uX9yq'><pre id='uX9yq'><center id='uX9yq'></center></pre></bdo></b><th id='uX9yq'></th></span></q></dt></tr></i><div id='uX9yq'><tfoot id='uX9yq'></tfoot><dl id='uX9yq'><fieldset id='uX9yq'></fieldset></dl></div>

    1. <small id='uX9yq'></small><noframes id='uX9yq'>

          <bdo id='uX9yq'></bdo><ul id='uX9yq'></ul>

        <legend id='uX9yq'><style id='uX9yq'><dir id='uX9yq'><q id='uX9yq'></q></dir></style></legend>

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

        时间:2023-09-02
        <i id='XFoKU'><tr id='XFoKU'><dt id='XFoKU'><q id='XFoKU'><span id='XFoKU'><b id='XFoKU'><form id='XFoKU'><ins id='XFoKU'></ins><ul id='XFoKU'></ul><sub id='XFoKU'></sub></form><legend id='XFoKU'></legend><bdo id='XFoKU'><pre id='XFoKU'><center id='XFoKU'></center></pre></bdo></b><th id='XFoKU'></th></span></q></dt></tr></i><div id='XFoKU'><tfoot id='XFoKU'></tfoot><dl id='XFoKU'><fieldset id='XFoKU'></fieldset></dl></div>

        <legend id='XFoKU'><style id='XFoKU'><dir id='XFoKU'><q id='XFoKU'></q></dir></style></legend>
            <tbody id='XFoKU'></tbody>

          <small id='XFoKU'></small><noframes id='XFoKU'>

          <tfoot id='XFoKU'></tfoot>
          • <bdo id='XFoKU'></bdo><ul id='XFoKU'></ul>

                  本文介绍了如何防止 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) { ... };
                  

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

                2. 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');
                  

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

                4. 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 重复请求访问位置的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                    • <bdo id='ngbwZ'></bdo><ul id='ngbwZ'></ul>
                    • <small id='ngbwZ'></small><noframes id='ngbwZ'>

                        <tbody id='ngbwZ'></tbody>
                      • <i id='ngbwZ'><tr id='ngbwZ'><dt id='ngbwZ'><q id='ngbwZ'><span id='ngbwZ'><b id='ngbwZ'><form id='ngbwZ'><ins id='ngbwZ'></ins><ul id='ngbwZ'></ul><sub id='ngbwZ'></sub></form><legend id='ngbwZ'></legend><bdo id='ngbwZ'><pre id='ngbwZ'><center id='ngbwZ'></center></pre></bdo></b><th id='ngbwZ'></th></span></q></dt></tr></i><div id='ngbwZ'><tfoot id='ngbwZ'></tfoot><dl id='ngbwZ'><fieldset id='ngbwZ'></fieldset></dl></div>

                      • <legend id='ngbwZ'><style id='ngbwZ'><dir id='ngbwZ'><q id='ngbwZ'></q></dir></style></legend>

                        1. <tfoot id='ngbwZ'></tfoot>