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

How can I open an external link in Safari not the app's UIWebView?

本文关键字:应用程序 UIWebView 链接 Safari 外部      更新时间:2023-09-26

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

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

但我不想打开所有链接的WebView,只是一些。

我希望我可以调用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中使用指定SCHEMA的所有URL。

恐怕我只能想到这些了。

希望这能帮助

更新:

代码应该放在MainViewControler中,至少对于cordova 2.2.0是这样。

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

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];

只需捕获javascript中所有具有target="_blank"的链接,并使用'_system'参数将它们传递给window.open。这将适用于iOS和Android。

$(document).on('click', 'a[target="_blank"]', function(ev) {
  var url;
  ev.preventDefault();
  url = $(this).attr('href');
  window.open(url, '_system');
});

根据@TDeBailleul的回答,这对我来说是有效的。基本上,任何后缀为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;
}

希望这能帮助其他人!

您可以(从phonegap 1.5.0开始)使用:

<a href="some://external/url" target="_blank">Click Me</a>

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

我认为user868766所指的是,为了使上述功能发挥作用,您需要将外部url列入白名单。我一直在开发的应用程序在本地浏览器中打开了新闻报道的来源,所以我们在白名单中使用了*,以确保我们不排除任何来源。

希望能有所帮助。

正确的方法是使用inAppBrowser插件

使用cordova CLI:安装

cordova plugin add org.apache.cordova.inappbrowser

然后,要在safari上打开链接,只需使用:

window.open('http://apache.org', '_system');

有一个新版本的插件托管在npm 上

从cordova CLI:安装

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;
}

在cordova 2.4+iOS 上测试

使用"_system",无需更新任何配置

http://docs.phonegap.com/en/2.3.0/cordova_inappbrowser_inappbrowser.md.html#InAppBrowser

target:要在(字符串)中加载URL的目标(可选,默认值:"_self")

_self-如果url在白名单中,则在Cordova WebView中打开,否则在InAppBrowser中打开_blank-始终在InAppBrowser中打开_系统-始终在系统web浏览器中打开

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

1-在外部主机(白名单)中添加您的链接。例如http://google.com
2-in Cordova.plist或Phonegap.list,将"OpenAllWhitelistURLsInWebView"从"是"更改为"否"
3-在您的应用程序中,将(target="_blank")添加到您的链接
实例

    <a href="http://google.com" target="_blank">Google.com</a>


非常感谢。

这对我有效,对有很大帮助

-(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;
}`
  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概述&list=UUefS6KLvFQVjhmL6hiBq4Sg

希望它能有所帮助!

iside 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 ]; }
}