0

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.

How can I achieve this?

I tried with:

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107

2 Answers2

1

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107
-1

The problem should be in your if statement, did you try to return false in all case yet? just to see if it worked or not?

func webView(WebViewNews: UIWebView!, shouldStartLoadWithRequest request: NSURLRequest!, navigationType: UIWebViewNavigationType) -> Bool {
    return false
}

And... it's rare but why not, did you add delegate to your webView?

Huy Tran
  • 815
  • 1
  • 12
  • 22
  • I did not add delegate to my webView... How can I do this? I can't just set the delegate to self... I'm sorry for that silly question I'm new to iOS Development. – Andreas Thaler Apr 05 '16 at 20:27
  • This is more of a comment than an answer. – JAL Apr 05 '16 at 20:44
  • @AndreasThaler you can follow this link https://ioswift.wordpress.com/2014/07/03/uiwebview-in-swift/ Basically there are 3 steps: - Extends your ViewController with UIWebViewDelegate - Assign webView.delegate = self - Write shouldStartLoadWithRequest delegate (you did it) – Huy Tran Apr 05 '16 at 20:45
  • @JAL sometimes we need to discuss to find out more, man :( btw, I found Andreas's problem in this comment. – Huy Tran Apr 05 '16 at 20:48