Ibz
Ibz

Reputation: 518

Restrict UIWebview to certain pages

I am trying to create a simple ipad app with a UIWebview that displays a form that a customer can fill in.. what i want to do is restrict the app so that it only allows the user to navigate to certain addresses.. (i.e. either something that allows the user to go to a specific address.. OR something that checks for specific keywords and allows/blocks them as appropriate..)

Could someone please show me how its done..

NB: its basically a googledocs form and i dont want to let the user navigate away from it.. (the user could easily click away and go elsewhere)

Thank you for reading :)

Upvotes: 2

Views: 2991

Answers (3)

Philipp Schlösser
Philipp Schlösser

Reputation: 5219

In the class that is your UIWebViewDelegate you can use something like this:

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *url = request.URL;
    NSString *urlString = url.absoluteString;

    //Check for your own url. You can use more advanced checking techniques of course :)
    NSRange range = [urlString rangeOfString:@"http://www.yourUrl.com"];
    if (range.location != NSNotFound) 
        return YES;
    else
        return NO;
}

Upvotes: 8

Youssef
Youssef

Reputation: 3592

Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:

Then check what is the URL that the UIWebView tires to load, and act consequently.

UIWebViewDelegate Reference

Upvotes: 1

jbat100
jbat100

Reputation: 16827

You can use the delegate method

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

to determine whether the UIWebView can load a given web page. Although that would mean knowing exactly which pages are allowed (if there are many this might not be convenient).

Upvotes: 1

Related Questions