UIWebView在Safari中打开链接

本文翻译自:UIWebView open links in Safari

I have a very simple UIWebView with content from my application bundle. 我有一个非常简单的UIWebView,其中包含我的应用程序包中的内容。 I would like any links in the web view to open in Safari instead of in the web view. 我希望Web视图中的任何链接都在Safari中打开,而不是在Web视图中打开。 Is this possible? 这可能吗?


#1楼

参考:https://stackoom.com/question/CALL/UIWebView在Safari中打开链接


#2楼

The other answers have one problem: they rely on the action you do and not on the link itself to decide whether to load it in Safari or in webview. 其他答案有一个问题:它们依赖于您执行的操作而不依赖于链接本身来决定是在Safari中还是在Webview中加载它。

Now sometimes this is exactly what you want, which is fine; 现在有时这正是你想要的,这很好; but some other times, especially if you have anchor links in your page, you want really to open only external links in Safari, and not internal ones. 但有些时候,特别是如果你的页面中有锚链接,你真的只想打开Safari中的外部链接,而不是内部链接。 In that case you should check the URL.host property of your request. 在这种情况下,您应该检查请求的URL.host属性。

I use that piece of code to check whether I have a hostname in the URL that is being parsed, or if it is embedded html: 我使用那段代码来检查我是否在正在解析的URL中有一个主机名,或者它是否嵌入了html:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    static NSString *regexp = @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9])[.])+([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexp];

    if ([predicate evaluateWithObject:request.URL.host]) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO; 
    } else {
        return YES; 
    }
}

You can of course adapt the regular expression to fit your needs. 您当然可以调整正则表达式以满足您的需求。


#3楼

If anyone wonders, Drawnonward's solution would look like this in Swift : 如果有人想知道,Drawnonward的解决方案在Swift中会是这样的:

func webView(webView: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    if navigationType == UIWebViewNavigationType.LinkClicked {
        UIApplication.sharedApplication().openURL(request.URL)
        return false
    }
    return true
}

#4楼

In my case I want to make sure that absolutely everything in the web view opens Safari except the initial load and so I use... 在我的情况下,我想确保Web视图中的所有内容都打开Safari,除了初始加载,所以我使用...

- (BOOL)webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
     if(inType != UIWebViewNavigationTypeOther) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
     }
     return YES;
}

#5楼

Add this to the UIWebView delegate: 将其添加到UIWebView委托:

(edited to check for navigation type. you could also pass through file:// requests which would be relative links) (编辑以检查导航类型。您还可以传递file://请求,这将是相对链接)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }

    return YES;
}

Swift Version: Swift版本:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        if navigationType == UIWebViewNavigationType.LinkClicked {
            UIApplication.sharedApplication().openURL(request.URL!)
            return false
        }
        return true
    }

Swift 3 version: Swift 3版本:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if navigationType == UIWebViewNavigationType.linkClicked {
        UIApplication.shared.openURL(request.url!)
        return false
    }
    return true
}

Swift 4 version: Swift 4版本:

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
    guard let url = request.url, navigationType == .linkClicked else { return true }
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    return false
}

Update 更新

As openURL has been deprecated in iOS 10: 由于openURL已在iOS 10中弃用:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
        if (navigationType == UIWebViewNavigationTypeLinkClicked ) {
            UIApplication *application = [UIApplication sharedApplication];
            [application openURL:[request URL] options:@{} completionHandler:nil];
            return NO;
        }

        return YES;
}

#6楼

In Swift you can use the following code: 在Swift中,您可以使用以下代码:

extension YourViewController: UIWebViewDelegate {
    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebView.NavigationType) -> Bool {
        if let url = request.url, navigationType == UIWebView.NavigationType.linkClicked {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
            return false
        }
        return true
    }

}

Make sure you check for the URL value and the navigationType. 确保检查URL值和navigationType。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值