問題描述
我的應(yīng)用中有一個(gè) WKWebView
,當(dāng)我開始瀏覽 www.google.com 或任何其他需要定位服務(wù)的網(wǎng)站時(shí),會(huì)出現(xiàn)一個(gè)彈出窗口,詢問是否允許訪問該位置即使我已經(jīng)同意分享我的位置,也可以使用設(shè)備.
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
.
我在網(wǎng)上找不到任何答案,所以任何想法都將不勝感激.
I couldn't find any answers on the web so any idea would be really appreciated.
推薦答案
事實(shí)證明這很困難,但可以做到.您必須注入 JavaScript 代碼來攔截對(duì) navigator.geolocation
的請(qǐng)求并將它們傳輸?shù)侥膽?yīng)用程序,然后使用 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
配置中,它會(huì)覆蓋navigator.geolocation
的方法.注入的 JavaScript 應(yīng)該如下所示:
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 應(yīng)該調(diào)用您的處理程序,如下所示:
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');
當(dāng)網(wǎng)頁請(qǐng)求位置時(shí),此方法將觸發(fā) userContentController(_:didReceive:)
,以便您的應(yīng)用知道網(wǎng)頁正在請(qǐng)求位置.像往常一樣在 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.
現(xiàn)在是時(shí)候使用 webView.evaluateJavaScript("didUpdateLocation({coords: {latitude:55.0, longitude:0.0}, timestamp: 1494481126215.0})")代碼>.當(dāng)然,你注入的 JavaScript 應(yīng)該有
didUpdateLocation
函數(shù)準(zhǔn)備好啟動(dòng)保存的成功處理程序.
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.
相當(dāng)長的算法,但它有效!
Quite a long algorithm, but it works!
這篇關(guān)于如何防止 WKWebView 重復(fù)請(qǐng)求訪問位置的權(quán)限?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!