2

我用jQuery Mobile 1.2爲iOS構建了一個Cordova 2.0應用程序。框架裏面。我已經成功地安裝了Childbrowser插件(在this versionthis guide感謝從這個角度來看,這些好人的幫助下,調用Childbrowser與目標=「_ blank」/ Cordova 2.0/jQuery Mobile 1.2.0

現在我可以直接用這個JavaScript中的頭一個onclick事件調用Childbrowser:

<script type="text/javascript"> 
    app.initialize(); 
    function launchCB() { 
    if(window.plugins.childBrowser != null) { 
     window.plugins.childBrowser.onLocationChange = function(loc){}; 
     window.plugins.childBrowser.onClose = function(){}; 
     window.plugins.childBrowser.onOpenExternal = function(){}; 
     window.plugins.childBrowser.showWebPage('http://www.google.de'); 
    } else { 
     alert('not found'); 
    } 
    } 
</script> 

,或者直接與例如

<a href="#" onclick="window.plugins.childBrowser.showWebPage('http://www.google.de');"> Google</a> 

現在我想開與屬性target="_blank"所有鏈接。所以我發現this thread,拿起日由查理Gorichanaz解決方案。

但是當我在iPhone模擬器中啓動應用程序時,我所得到的只是sandclock或jQuery手機死亡的旋轉輪。

我很高興爲每一個有用的建議,我從來沒有編碼此應用程序之前。這是我的index.html

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <meta name = "format-detection" content = "telephone=no"/> 
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width;" /> 
    <link rel="stylesheet" type="text/css" href="css/index.css" /> 
    <title>Cordova</title> 
    <script type="text/javascript" src="cordova-2.0.0.js"></script> 
    <script type="text/javascript" src="ChildBrowser.js"></script> 
    <gap:plugin name="ChildBrowser" /> <!-- latest release --> 
    <script type="text/javascript" charset="utf-8" src="EmailComposer.js"></script> 
    <script type="text/javascript" src="js/index.js"></script> 
    <script type="text/javascript"> 
     app.initialize(); 
     function launchCB() { 
     if(window.plugins.childBrowser != null) { 
      window.plugins.childBrowser.onLocationChange = function(loc){}; 
      window.plugins.childBrowser.onClose = function(){}; 
      window.plugins.childBrowser.onOpenExternal = function(){}; 
      window.plugins.childBrowser.showWebPage('http://www.google.de'); 
     } else { 
      alert('not found'); 
     } 
     } 
     /* 
     var args; 
     cordova.exec(null, null, "EmailComposer", "showEmailComposer", [args]); 
    */ 
    </script> 
    <!-- jQuery mobile --> 
    <link type="text/css" rel="stylesheet" media="screen" href="jqm/jquery.mobile-1.2.0-alpha.1.min.css"> 
    <link type="text/css" rel="stylesheet" media="screen" href="jqm/Changes.css"> 
    <script type="text/javascript" src="jqm/jquery-1.7.2.min.js"></script> 
    <script> 
    // for using childbrowser to open pdf on remote sites 
    $(document).bind("mobileinit", function() { 
     $.mobile.allowCrossDomainPages = true; 
    } 
    ); 
    // the function i want to implement 
    $(document).bind("pageinit", function() { 
    onDeviceReady(); 
    }); 
    function onDeviceReady() { 
    var root = this; 
    cb = window.plugins.childBrowser; 
    if (cb != null) { 
     $('a[target="_blank"]').click(function(event) { 
     cb.showWebPage($(this).attr('href')); 
     event.preventDefault(); 
     event.stopImmediatePropagation(); 
     return false; 
     }); 
    } 
    } 
    // don't know is this thing is right in place... 
    document.addEventListener("deviceready", onDeviceReady, false); 
</script> 
<script src="jqm/jquery.mobile-1.2.0-alpha.1.min.js"></script> 
</head> 
<body> 
    <section data-role="page" id="home" data-theme="a" data-position="fixed"> 
    <div data-role="header"> <!-- header --> 
     <h1>Test</h1> 
     <div style="position:absolute; top:0px; right:5px;"> 
     <a href="#about" data-transition="pop"> 
      <img src="images/schlange-sw.png" alt="Schlange"> 
     </a> 
     </div> 
    </div> 
    <!-- /header --> 
    <div data-role="content"> <!-- content --> 
     <a id="domainbut" onclick='launchCB()'>Working </a> 
     <a href="http://www.google.de/" target="_blank" data-role="button" data-inline="true"> not working </a> 
    </div> 
    <!-- content --> 
    <div data-role="footer" data-theme="a" data-position="fixed"></div> 
    </section> 
    <section data-role="dialog" id="about" data-close-btn-text="Close This Dialog"> 
    <div data-role="header"> 
     <h1>Über</h1> 
    </div> 
    <div data-role="content"> 
     <h1 style="text-align: center;"></h1> 
     <div align="center"> 
    </div> 
    <p style="text-align: center;">The owner of this app</p> 
    <button onclick="cordova.exec(null, null, 'EmailComposer', 'showEmailComposer', [args]);">Compose Email</button> 
    <p> 
     <a href="#home" data-role="button" data-rel="back">OK</a> 
    </p> 
    </div> 
</section> 
</body> 
</html> 

在此先感謝您。

問候

Kieke

+0

你得到控制檯上的錯誤? – Littm

回答

0

在你Cordova.plist,你有OpenAllWhitelistURLsInWebView = YES並所有域(如www.google.com,是localhost)要連接到在你的ExternalHosts列表?

如@Littm所述,查看Xcode調試器的控制檯,您將看到您的鏈接是否因爲不在白名單而被阻止。

您也可以檢查您的system.log,tail /var/log/system.log執行後的任何錯誤。

+0

感謝您的建議,但沒有白名單拒絕。我在Externalhosts中設置了一個Asterisk,以便允許所有請求。我認爲在我的代碼中有一個錯誤,但是我沒有在控制檯中得到任何錯誤。我會很高興,如果這將是一個拒絕,但我會檢查你提到的日誌。 – Kieke

+0

@Kieke,檢查上面的答案,它會告訴你如何手動打開一個給定if條件的Safari窗口。不需要插件。 – sonjz

1

@Kieke,如果您決定取消ChildBrowser,我發現以下工作適合我。

注意:假設你正在使用PhoneGap的2.x的

在你Cordova.plist設置OpenAllWhitelistURLsInWebView = YES並設置ExternalHosts名單,*是罰款。任何你想要webview不要阻止(在Safari或應用程序中查看)必須在您的ExternalHosts列表中。

在你MainViewController.m下面的代碼添加至底部,您可以手動重定向任何URL Safari瀏覽器,看到www.loadDomainInSafari.comif聲明:

- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSURL *requestURL =[ [ request URL ] retain ]; 
    NSString *host = [ [ requestURL host] retain ]; 

    // if YES, directs to WebView 
    // otherwise, takes OpenAllWhitelistURLsInWebView setting 

    // if www.loadDomainInSafari.com, open in Safari by explictly stating NO. 
    // otherwise take OpenAllWhitelistURLsInWebView setting of YES 
    if ([host isEqualToString:@"www.loadDomainInSafari.com"]) { 
     return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease] ]; 
    } 
    [ requestURL release ]; 
    return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; 
} 
+0

在別處沒有看到這個解決方案。謝謝! –