0

我們目前正在開發使用BlackBerry WebWorks SDK的phonegap應用程序,目前我們無法訪問或控制WebView及其URL。BlackBerry 10 WebWorks BlackBerry 10 PhoneGap WebView URL

它基本上加載了我不想要的Index.html頁面。我想在服務器上加載一些外部URL,然後一旦驗證就加載www文件夾的index.html。這不起作用。任何想法如何控制WebView URL?

感謝, ANKIT塔納

回答

0

我只是開發了WebWorks的2.1 /科爾多瓦的應用程序,並遇到了同樣的挑戰。

首先,我的目標是跨平臺,所以我使用InAppBrowser插件(雖然說實話,我不確定是否必須手動包含)。

BlackBerry 10 WebView似乎不支持loadstart和exit事件,因此您必須使用計時器手動檢查WebView的狀態。事情是這樣的:

var VIEWER = window.open('http://myauthenticationpage/login.html', '_blank', 'location=no,menubar=no,titlebar=no,toolbar=no,status=no'); 

//Start a timer to check for a URL change in the WebView 
var AuthTimer = setInterval(function() { 

    //Handle your failure case your own way, but stopping this loop is a good idea if the WebView isn't open 
    if ((typeof VIEWER === 'undefined') || (typeof VIEWER.document === 'undefined')) { 
     clearInterval(AuthTimer); 
     return; 
    } 

    //Get the current URL of the WebView 
    var url = VIEWER.document.URL; 

    //Process the URL in some way, like to extract a token 
    //You can also access the DOM of the loaded page, as another option 
    if (url.indexOf('authorizedUser=') > 0) { 
     //Extract the username, which my server appends in the query string 
     var user = url.substring(url.indexOf('authorizedUser=') + 15); 

     //I store the user token in localStorage 
     localStorage.setItem('user', user); 

     //Close the viewer and stope this timer 
     clearInterval(AuthTimer); 
     VIEWER.close(); 
    } 
}, 20); 

這樣做了以後,你可能需要做一個AJAX調用服務器獲取用戶令牌或其他一些信息...取決於具體的實現。

所以當你的應用程序啓動時,你的第一頁應立即做一個檢查:我有一個存儲的用戶令牌嗎?如果沒有,請啓動上述過程。繼續啓動它直到我有一個存儲的用戶令牌。當我有一個令牌時,從服務器開始加載用戶數據,或從www /加載index.html,或者任何你想做的事情。

這對我和設備處理得很好。

注爲Android/iOS版

如果你決定把它跨平臺,Android或iOS,你可以做到這一點使用事件,而不是:

var VIEWER = window.open('http://myauthenticationpage/login.html', '_blank', 'location=no,menubar=no,titlebar=no,toolbar=no,status=no'); 

VIEWER.addEventListener('exit', function(e) { 
    //The webview closed! 
    if (user == null) { 
     //We still don't have a username 
     setTimeout(function() { 
      //Kick open the webview again, to try again for a username for example 
      //(Might want to wrap all this in a function) 
      VIEWER = window.open('http://myauthenticationpage/login.html', '_blank', 'location=no,menubar=no,titlebar=no,toolbar=no,status=no'); 
     }, 50); 
    } 
}); 

VIEWER.addEventListener('loadstart', function(e) { 
    //The webview has started loading a new page 
    var url = e.url; 
    var user = null; 

    if (url.indexOf('authorizedUser=') > 0) { 
     user = url.substring(url.indexOf('authorizedUser=') + 15); 

     localStorage.setItem('user', user); 

     VIEWER.close(); 
    } 
}); 

使用這種技術,你的網頁當發生成功登錄時,需要'回顯'查詢字符串或響應DOM中的一些數據。我認爲對於大多數授權實現,只需在您的請求中設置一個虛擬回調URL,然後在WebView url中查看回調URL。

我希望這有助於!

約翰