防止用户在Swift中离开网站 [英] Prevent user from leaving website in Swift

查看:100
本文介绍了防止用户在Swift中离开网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个iPad-App中创建一个显示网站的WebView.我想防止用户单击链接到其他网站(例如Facebook,Twitter等)的链接,但是应该允许他自由地在当前网站上移动.

I want to create a WebView in an iPad-App, that displays a website. I want to prevent the user to click on links that link to other websites (e.g. Facebook, Twitter, ...) but he should be allowed to move around on the current website freely.

我该如何实现?

我尝试过:

func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    let newWebsite:String = (request.URL?.path)!
    print(newWebsite)
    if newWebsite.rangeOfString("float-schwabing") != nil{
        return true;
    } else {
        return false;
    }
}

虽然没有用.我仍然可以访问其他网站.

It didn't work though. I can still access other websites.

如果有人可以帮助我,那将很棒.

It would be awesome, if somebody could help me with this.

推荐答案

完成后,您应该可以在网站中导航,您可能会有一些内部链接.试试下面的代码,而不是今天做的事情:

As you have done you should be able to navigate within your website, you might have some internal links. Try the below code instead of how you have done it today:

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    if String(request.URL!).rangeOfString("YOUR WEBSITE") != nil{
        return true
    }
    else{
        return false
    }
}

您基本上可以检查请求网址中是否包含您的网站名称,如果是,则可能要打开该链接,否则您就不会打开.

You basically check if the request url contains your websites name, if it does you might want to open that link otherwise you don´t.

这篇关于防止用户在Swift中离开网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆