2013-04-30 138 views
3

我試圖在Chrome瀏覽器打包的應用程序中瀏覽webview中的地理位置,以便正確運行我的應用程序。我嘗試了幾種方法來獲取manifest.json中的權限並注入腳本,但它不起作用,並且不顯示任何錯誤消息。Chrome瀏覽器內的web瀏覽器中的GeoLocation打包應用程序

有人可以給我一個燈光或解決方案來獲得許可,並顯示我的地理位置?

回答

5

通常在普通網頁中需要權限的一些功能也可以在web視圖中使用。然而,包含webview的應用程序需要明確授權它,而不是正常的彈出窗口「網站xyz.com想知道您的物理位置 - 允許/拒絕」。這是它是如何工作的:

  • 無需更改web視圖內的網頁;

  • 在應用中,你聽的<webview>元素permissionrequest事件:

webview.addEventListener('permissionrequest', function(e) { 
    if (e.permission === 'geolocation') { 
    e.request.allow(); 
    } else { 
    console.log('Denied permission '+e.permission+' requested by webview'); 
    e.request.deny(); 
    } 
}); 

有一點要注意的是,請求並不需要馬上處理。只要您在permissionrequest事件中調用preventDefault並保持事件對象不被垃圾收集,就可以在允許或拒絕之前做任何您需要做的事情。如果您需要執行任何異步操作(如轉到存儲以檢查是否允許請求權限的URL),這非常有用。

例如:

webview.addEventListener('permissionrequest', function(e) { 
    if (e.permission === 'geolocation') { 
    // Calling e.preventDefault() is necessary to delay the response. 
    // If the default is not prevented then the default action is to 
    // deny the permission request. 
    e.preventDefault(); 
    setTimeout(function() { decidePermission(e); }, 0); 
    } 
}); 

var decidePermission = function(e) { 
    if (e.url == 'http://www.google.com') { 
    e.request.allow(); 
    } 
    // Calling e.request.deny() explicitly is not absolutely necessary because 
    // the request object is managed by the Javascript garbage collector. 
    // Once collected, the request will automatically be denied. 
    // If you wish to deny immediately call e.request.deny(); 
} 
  • 另外請注意,您的應用需要也要求相應的權限:
"permissions": ["geolocation"], 

webview sample具有其他權限的詳細代碼,例如pointerLock和媒體捕獲。更

+0

UPDATE:添加@ user2296820關於延遲審批/拒絕許可請求的信息 – mangini 2013-04-30 21:52:00

+0

謝謝,問題已解決! =) – brunowindrop 2013-05-01 07:54:47

+0

早上好! Mangini我以爲我已經解決了你的代碼的問題,但在我發佈應用程序來安裝我的一些朋友沒有工作。 我意識到只有在鉻金絲雀工作...做了一些測試,以強制顯示在正常位置可以得到,但它根本不顯示。 你知道輸入一個原因嗎? 鏈接網上商店:https://chrome.google.com/webstore/detail/localizeime/ihappndkmffngabclkkpebgnbchlbhjg?utm_source=plus 那裏,你可以找到github上的文件 – brunowindrop 2013-05-02 13:53:28

0

詳細一點:

的響應並不需要,只要你preventDefault()以立即進行。默認操作是拒絕權限請求。

webview.addEventListener('permissionrequest', function(e) { 
    if (e.permission === 'geolocation') { 
    // Calling e.preventDefault() is necessary to delay the response. 
    // If the default is not prevented then the default action is to 
    // deny the permission request. 
    e.preventDefault(); 
    setTimeout(function() { decidePermission(e); }, 0); 
    } 
}); 

var decidePermission = function(e) { 
    if (e.url == 'http://www.google.com') { 
    e.request.allow(); 
    } 
    // Calling e.request.deny() explicitly is not absolutely necessary because 
    // the request object is managed by the Javascript garbage collector. 
    // Once collected, the request will automatically be denied. 
    // If you wish to deny immediately call e.request.deny(); 
} 
+0

謝謝,我已將此添加到我的回覆中。 – mangini 2013-04-30 21:53:17

相關問題