Javascript 如何在Safari中打开外部链接而不是应用程序';UIWebView?

Javascript 如何在Safari中打开外部链接而不是应用程序';UIWebView?,javascript,ios,cordova,Javascript,Ios,Cordova,我有一个Phonegap(cordova)应用程序,我想在Phonegap WebView中加载一些外部网页,我还有其他外部网页,当用户激活它们时,我想在safari中加载它们 通常,大多数人都会遇到在WebView中打开外部链接的问题。将OpenAllWhitelistURLsInWebView设置为YES(在Cordova.plist/Phongap.plist中)可以解决这个问题 但是我不想打开网络视图中的所有链接,只是一些链接 我希望我可以直接调用window.open()http://

我有一个Phonegap(cordova)应用程序,我想在Phonegap WebView中加载一些外部网页,我还有其他外部网页,当用户激活它们时,我想在safari中加载它们

通常,大多数人都会遇到在WebView中打开外部链接的问题。将OpenAllWhitelistURLsInWebView设置为YES(在Cordova.plist/Phongap.plist中)可以解决这个问题

但是我不想打开网络视图中的所有链接,只是一些链接

我希望我可以直接调用
window.open()http://someexternalsite“)
在Safari和
window.parent.location.href=”中打开http://mysite“
在WebView中打开它


你知道怎么做吗?

如果你想在safari中打开的链接都包含一个公共字符串,你可以使用下一段代码

- (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:@"SCHEME"]) {
        [[UIApplication sharedApplication] openURL:url];
        return NO;
    }
    else {
        return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
    }
}
放置在
AppDelegate.m
中的此代码将打开在Safari中使用指定方案的所有URL

恐怕我只能想到这些了

希望这有帮助

更新:

至少对于cordova 2.2.0,代码应放在MainViewControler中

最初对该方法进行了注释。我不得不用它重定向谷歌地图链接:

NSRange isGoogleMaps = [[url absoluteString] rangeOfString:@"maps.google.com" options:NSCaseInsensitiveSearch];
NSRange isGoogleTerms = [[url absoluteString] rangeOfString:@"terms_maps.html" options:NSCaseInsensitiveSearch];

if(isGoogleMaps.location != NSNotFound || isGoogleTerms.location != NSNotFound ) {
        [[UIApplication sharedApplication] openURL:url];
       return NO;
}
else 
    return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType];
您可以(从phonegap 1.5.0起)使用:


这将导致phonegap启动本机浏览器

我认为user868766所指的是,要使上述内容起作用,您需要将外部url放在白名单上。 我一直在开发的应用程序在本地浏览器中打开了新闻故事的来源,因此我们在白名单中使用了*以确保我们不排除任何来源


希望能有所帮助。

根据@tdebaileul的回答,这对我来说是有效的。基本上,任何后缀为PDF的链接,或者如果它是我想在Safari中打开的特定页面,或者如果它不是www.example.com/*(外部链接)中的页面,都将在新窗口中打开:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    // Open PDF files, Specific Pages, and External Links (not beginning with http://www.example.com) in Safari.app
    if (
        (navigationType == UIWebViewNavigationTypeLinkClicked) && 
        ([[[request URL] absoluteString] hasSuffix:@"pdf"] || [[[request URL] absoluteString] hasPrefix:@"http://www.example.com/specific-page.php"] || ![[[request URL] absoluteString] hasPrefix:@"http://www.example.com"])
        ) { 

        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    } 

    return YES;
}

希望这能帮助别人

如果您想在safari中打开外部url,我认为这很有用:
如果您使用的是在ios6中测试过的phonegap,这是100%保证的解决方案。
要在safari中打开外部url,请执行以下操作:

1-在外部主机中添加链接(白名单)。例如
2-在Cordova.plist或Phonegap.plist中,将“OpenAllWhitelistURLsInWebView”从“是”更改为“否”
3-在应用程序中,将(target=“\u blank”)添加到链接中
例子




谢谢。

在cordova 2.4+iOS上测试

使用“\u系统”,无需更新任何配置

目标:加载URL的目标(字符串)(可选,默认值: “_self”)

_如果url在白名单中,self将在Cordova WebView中打开,否则将在InApp浏览器中打开 _空白-始终在InApp浏览器中打开 _系统-始终在系统web浏览器中打开


只需捕获javascript中具有
target=“\u blank”
的所有链接,并将它们传递给window.open(使用“\u system”参数)。这将在iOS和Android上都适用

$(document).on('click', 'a[target="_blank"]', function(ev) {
  var url;

  ev.preventDefault();
  url = $(this).attr('href');
  window.open(url, '_system');
});

这对我来说很有用

-(void)viewDidLoad
{
   [super viewDidLoad];
    ////////////////////////
    NSString *urlAddress = @"http://www.playbuzz.org/";
    //NSURL *myURL = [NSURL URLWithString:urlAddress];
    myWebview.delegate = (id)self;
   [myWebview loadRequest:[NSURLRequest requestWithURL:[NSURL     URLWithString:urlAddress]]];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {


    //  open External Links (not beginning with www.playbuzz.org/ in Safari.app
    if (
        (navigationType == UIWebViewNavigationTypeLinkClicked) &&
        ( ![[[request URL] absoluteString] hasPrefix:@"http://www.playbuzz.org/"])
        ) {

        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }

   //open Internal links in uiwebview
   return YES;
}`
  • 将target=“\u blank”添加到链接中。 即:

  • 我制作了一个快速视频,解释如何解决此问题:

    希望有帮助

    对xcode进行加密

    //将代码放入/Classes/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 ]; }
    }
    

    正确的方法是使用

    使用cordova CLI安装它:

    cordova plugin add org.apache.cordova.inappbrowser
    
    cordova plugin add cordova-plugin-inappbrowser
    
    然后,要在safari上打开链接,只需使用:

    window.open('http://apache.org', '_system');
    
    npm上有一个托管的

    要从cordova CLI安装它,请执行以下操作:

    cordova plugin add org.apache.cordova.inappbrowser
    
    cordova plugin add cordova-plugin-inappbrowser
    
    要在safari上打开网站,您可以使用

    cordova.InAppBrowser.open('http://apache.org', '_system');
    
    或者,如果要继续使用window.open(与旧版本类似),您可以在设备就绪事件上执行此操作:

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.open = cordova.InAppBrowser.open;
    }
    

    我希望有更好的方法,但看起来电话差距不支持我所寻找的开箱即用。谢谢我也想用这个。为了让它工作,您只需要在该文件中输入这些内容(而在任何其他文件中都不输入任何内容)吗?我只想在safari中打开所有外部链接
    target=“_blank”
    。碰巧有人知道用android实现这一点的方法吗?如果([request.URL.absoluteString rangeOfString:@“somedomain.com”].location!=NSNotFound){[[UIApplication sharedApplication]openURL:request.URL];返回否;}这很有效。我从其他帖子中感觉到Cordova 2.0+将支持在safari中打开空白目标,但还没有测试过。我正在开始我的android版本,所以我还需要让它工作。如果行得通,我就报告back@AdamD据phonegap的人说,你的建议应该行得通&我不知道为什么它对我不起作用,可能是因为我使用的是javascript window.open,而不是通过链接。此外,在那篇文章中还说,如果你不将URL添加到白名单中,它将自动在Android的外部浏览器中打开(该浏览器适用于4.1,但由于某些原因,不适用于早期版本,如2.2)。在iOS上,您仍然需要将其添加到白名单中,以便在webview或out中对url执行任何操作。我尝试了此操作,但它似乎没有执行任何操作。相反,我尝试在javascript中执行此操作,但它似乎没有在Windows中执行任何操作some://external/url“,”空白“);我还将webview中的open all whitelist URL设置为true,因为我的主应用程序实际上是一个webapp,phonegap应用程序只是用作登录页面,经过身份验证后,会重定向到webapp。如果客户端需要更新,它应该重定向到另一个位置(而不是webapp)。@MatthewLevine您仍然需要在
    Cordova.plist
    中允许外部URL的域。如果您需要允许任何域,那么就有一个
    ExternalHosts
    条目,其中只有
    *
    是的,这对我也适用。安卓(2.2-present)也能用上类似的东西吗?是的,在iOS上也能用,但在安卓上效果不太好。我是霍皮人
    cordova plugin add cordova-plugin-inappbrowser
    
    cordova.InAppBrowser.open('http://apache.org', '_system');
    
    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.open = cordova.InAppBrowser.open;
    }