2014-09-09 4 views
0

PhoneGap (특히 PhoneGap Build) 및 InAppBrowser를 사용하여 반응이 빠른 온라인 웹 사이트를 사용하기 쉬운 시스템으로 포장하는 앱을 만들고 있습니다.PhoneGap/Cordova에서 InAppBrowser로 기본 브라우저에서 링크 열기

앱은 시스템의 기본 브라우저에서 외부 링크를 여는 기능 하나를 제외하고 잘로드되고 작동합니다.

이것은 95 % 작동하지만 특정 경우에 링크가 리디렉션되는 문제를 일으 킵니 다 (예 : http://www.facebook.comhttps://www.facebook.com으로 리디렉션 됨 https://m.facebook.com으로 리디렉션 됨).

내 코드에서는 URL이 구성된 도메인과 다름을 감지하고이를 새 시스템 창에서 열고 다른 두 리디렉션을로드하고 앱 내에서로드합니다.

원하는 작업은 시스템의 기본 브라우저에서 링크를 열고 추가 처리 (리디렉션 포함)를 모두 중지하는 것입니다.

웹 페이지의 라이브이기 때문에 HTML 페이지의 링크에서 _system으로 변경하는 것만으로는 앱의 외부에 있기 때문에 작동하지 않습니다.

모든 조언과 조언을 부탁드립니다.

코드의 가장 중요한 발췌

은 다음과 같습니다 폰갭의 응용 프로그램 내에서

:

function isExternal(url) { 
    var match = url.match(/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/); 
    if (typeof match[1] === "string" && match[1].length > 0 && match[1].toLowerCase() !== location.protocol) return true; 
    if (typeof match[2] === "string" && match[2].length > 0 && match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host) return true; 
    return false; 
} 

function goToPage(url, external){ 
    if(external) 
     //Go to new URL with appended hash flag 
     window.location=encodeURI(url)+'#appExt'; 
    else{ 
     //Show loading screen 
     $('#app-overlay').css('display','block'); 
     //Wait for 100ms then change page, so the loading screen gets time to show 
     setTimeout(function(){window.location=encodeURI(url)},100); 
    } 
} 

$('a').on('click',function(e){ 
    e.preventDefault(); 

    if(isExternal($(this).attr('href'))){ 
     //If the URL is an external one, the app.js file will open the url in a new window after setting the appExt hash flag 
     if(debug) alert('Stopping navigation from injection.js as external link detected'); 
     goToPage($(this).attr('href'), true); 
    } 
    else{ 
     //Otherwise, continue normally 
     if(debug) alert('Regular link clicked'); 
     goToPage($(this).attr('href'), false); 
    } 

    //Cancel the default link operation, as we've already handled it 
    return false; 
}); 
: 자바 스크립트 파일에서

//Called on deviceready 
function launchSite(){ 
    // Get Webpage in InAppBrowser (using the _blank target) and hide it until loaded 
    iabRef = window.open(url+'?inApp=true'), '_blank', 'location=no,hidden=yes'); 

    //Attach listener for external link intercepting 
    iabRef.addEventListener('loadstart', iabLoad); 
    //Attach listener for loading complete 
    iabRef.addEventListener('loadstop', iabLoaded);  
} 

//Check if links are external, if they are, open them in the system's default browser 
function iabLoad(event){ 
    if(debug) alert('Page load request for ' + event.url); 
    //Check the link to see if it is external, which is flagged by #appExt from injection.js 
    if(event.url.indexOf('appExt') > -1){ 
     //Open the window using the _system target to use the system's default browser 
     if(debug) alert('Opening '+event.url + ' in system window') 
     window.open(event.url,'_system'); 

     if(debug) alert('Stopping anything more'); 
     //Mobile redirects keep triggering load events from initial link, stop the process now. 
     window.stop(); // <-Does not stop 
    } 
    else{ 
     if(debug) alert('Opening normally without interference'); 
    } 
} 

//If window was hidden whilst loading, show it now that it has loaded 
function iabLoaded(event){ 
    if(debug) alert('Showing IAB window'); 
    iabRef.show(); 
} 

는 반응 웹 사이트에 포함

업데이트 : 계속 조정 및 재 컴파일이 더 이상 나를 필요로하지 않습니다. 누구든지 나를 위해이 일을 주시 할 수 있습니까?

+0

동일한 문제가 발생했습니다. 해결책을 찾았습니까? – Purva

답변

0

window.open 브라우저에서 링크를 열려고합니까? 우리의 응용 프로그램에서 우리는 나는 그것이 최선의 대답 잘 모르겠어요이

<span class="item_link"><a href="#" onclick="window.open(\''+ YourUrl +'\', \'_system\');">' + MoreLink + '</a></span> 
+0

죄송합니다. 외부 사이트이므로 HTML의 모든 외부 링크를 수정할 수 없으므로 프로그래밍 방식으로 수행해야합니다. – chrismcb

1

처럼 그것을 할,하지만 난합니다 (startLoad 이벤트를 통해) 방문 마지막 URL을 추적하여 "해결"때 누군가가 이미로드 된 사이트 (startLoad 이벤트 사용)와 다른 사이트에 연결되는 것을보고, 외부 URL에서 _system을 사용하여 window.open을 수행 한 다음 InAppBrowser의 페이지를 마지막 URL로 다시 변경합니다. (나는 계속 추적하고있다)에 있었다.

가장 우아하지는 않지만 다른 방법을 찾지 못해서 나쁘지는 않습니다. cordova 3.6.3 (다음 주에 업그레이드하기를 희망 함)과 inAppBrowser 0.6.0을 사용합니다. 다른 사람이 유용하다고 생각하면 코드 스 니펫을 게시 할 수 있습니다.

+0

안녕하세요 저는 같은 문제에 직면하고 있습니다. 동일한 단계를 수행 할 수 있도록 일부 코드를 입력 할 수 있습니까? – Purva

+0

죄송합니다.이 프로젝트를 포기했습니다. 아마도 larry-l이 도와 드릴 수 있습니다. – chrismcb

관련 문제