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

在别处没有看到这个解决方案。谢谢! –