xcode 将 UIWebView 限制为特定 URL (Swift)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27464407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:21:25  来源:igfitidea点击:

Limit UIWebView to specific URL (Swift)

iosxcodeuiwebviewsafari

提问by MattFiler

I'm trying to develop an iOS app that features a UIWebView in Swift, however I need it to only work with one domain, and any external links clicked on in that UIWebView to be opened in Safari. (E.G. All links to http://example.comwill open within the UIWebView, but a link to http://twitter.comwould open in Safari).

我正在尝试开发一个在 Swift 中具有 UIWebView 功能的 iOS 应用程序,但是我需要它只适用于一个域,并且在该 UIWebView 中单击的任何外部链接都可以在 Safari 中打开。(EG 所有链接都http://example.com将在 UIWebView 中打开,但链接http://twitter.com将在 Safari 中打开)。

I have found a couple of solutions online but the only Swift version (found here: UIWebView open links in Safari) works to open every link in Safari, I just want external ones to do so.

我在网上找到了几个解决方案,但唯一的 Swift 版本(在这里找到:UIWebView open links in Safari)可以打开 Safari 中的每个链接,我只希望外部链接可以这样做。

Could anyone help? Thanks.

有人可以帮忙吗?谢谢。

回答by Ben H

As @MattFlier's solution is correct but doesn't compile with the latest Swift compiler. It's important to unwrap the NSURLRequest URL property so the updated solution is:

由于@MattFlier 的解决方案是正确的,但不能使用最新的 Swift 编译器进行编译。解包 NSURLRequest URL 属性很重要,因此更新的解决方案是:

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

And yes, to hook this up you can Control+Drag your UIWebView to the UIViewController that is representing the view, and then make sure the delegate function is named the same as the "IBOutlet weak var", like so

是的,要连接它,您可以 Control+Drag 你的 UIWebView 到代表视图的 UIViewController,然后确保委托函数的名称与“IBOutlet weak var”相同,就像这样

What this looks like

这看起来像什么

You alsoneed to make sure your view controller implements UIWebViewDelegate

需要确保您的视图控制器实现UIWebViewDelegate

class OfferDetailViewController : UIViewController, UIWebViewDelegate
{
    ...

And then in your viewDidLoad()method you also need to assign the delegate like so:

然后在您的viewDidLoad()方法中,您还需要像这样分配委托:

override func viewDidLoad() {
    self.webView.delegate = self;
}

回答by MattFiler

I must be getting better at Swift than I thought...

我一定比我想象的更擅长 Swift...

Modifying the solution that I found originally (linked in the question), I came up with this alternative that allows through my URL, but opens any external URLs in Safari.

修改我最初找到的解决方案(在问题中链接),我想出了这个替代方案,它允许通过我的 URL,但在 Safari 中打开任何外部 URL。

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

Don't forget to delegate this code from the UIWebView to the View Controller, and it should work fine (just as long as you replace example.comwith your URL.

不要忘记将此代码从 UIWebView 委托给视图控制器,它应该可以正常工作(只要您替换example.com为您的 URL。

Please vote up this comment, as I have spent all day searching through tutorials and forums, and think that my final solution is a pretty clean one!

请投票支持这条评论,因为我花了一整天时间搜索教程和论坛,并认为我的最终解决方案是一个非常干净的解决方案!