2013-03-11 88 views
1

在将我的应用程序更新到Cordova 2.5.0之前,我的基本链接已打开到Safari中。现在它只是加载在我的应用程序视图中,而不是加载到Safari。Phonegap - 在移动Safari中打开链接

我有下面的代码: Contact Us by visiting, <a href="http://site.com/contact.php"> our website! </a>

我如何能解决这个问题的任何想法。

欲了解更多信息:当我们从Cordova/Phonegap.plist切换到config.xml时,这一切都开始了。

+0

更新前使用旧版本phonegap? phonegap使用webview(webkit)并且无法使用safari。 – omid 2013-03-11 22:22:53

+0

是的,当你打开一个链接(带有http://)时,它会在Safari中打开。就像它会跳转应用程序。 – David 2013-03-11 23:26:57

回答

1

最近我一直在摔跤这个问题,并提出了一个至少对我有用的答案。 Cordova 2.6和JQuery Mobile。

<a href="http://yoursite.something.com" target="_system"> the external link </a> 

有相当数量的答案使用target =“_ blank”。但是,这只是在他们的WebView中打开链接。希望这对你有用,就像它为我做的那样!

+0

这对我不起作用。我需要使用'onclick =“window.open('http://example.com','_system')”'使它工作。 – jvannistelrooy 2014-12-22 13:28:40

5

1)在Safari中打开链接

将代码粘贴在MainViewController.m文件InAppBrowser

function OpenLink() 
        { 
        alert(adlink); //adlink: http://www.google.com 
         var ref = window.open(adlink, '_blank', 'location=yes');//adlink is url 
         var myCallback = function() { alert(event.url); } 
         ref.addEventListener('loadstart', myCallback); 
         ref.removeEventListener('loadstart', myCallback); 
          } 

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

    // Intercept the external http requests and forward to Safari.app 
    // Otherwise forward to the PhoneGap WebView 
    if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) { 
     [[UIApplication sharedApplication] openURL:url]; 
     return NO; 
    } 
    else { 
     return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; 
    } 
} 

2)打开链接

+1

这对PhoneGap的最新版本适用于我。在iOS模拟器中没有其他工作。 +1 – 2013-08-30 06:17:59

+0

我到处寻找这样的东西,谢谢 – 2013-09-30 03:30:09

+0

我正在构建的特定应用程序在页面上有一个iframe,并且它也在safari中打开。有这个简单的解决办法吗? – 2013-10-01 05:18:18

4
  1. 将target =“_ blank”添加到您的链接。 即:

    <a href="http://www.brandonbrotsky.com/" target="_blank"></a> 
    
  2. 确保访问具有的原点* />在你的config.xml文件(确保它的一个应用程序目录的根目录,上面的WWW文件夹 即:

    <access origin="*" /> 
    
  3. 下面的代码添加到MainViewController.m

    - (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    { 
        NSURL *url = [request URL]; 
    
        // Intercept the external http requests and forward to Safari.app 
        // Otherwise forward to the PhoneGap WebView 
        if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) { 
         [[UIApplication sharedApplication] openURL:url]; 
         return NO; 
        } 
        else { 
         return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; 
        } 
    } 
    

我做了一个简短的视频,解释如何来解决这个问题:

http://www.youtube.com/watch?v=zqbjXSnAR-Q&feature=c4-overview&list=UUefS6KLvFQVjhmL6hiBq4Sg

希望它能帮助!

+0

快速解释或总结将非常赞赏StackOverflow。 – staticVoidMan 2013-12-07 22:16:22

+1

刚刚提交了一个编辑。谢谢! – 2013-12-10 20:42:09

+0

3号就是这样。伟大的工作布兰登 – 2014-01-04 03:44:15

相关问题