Restrict UIWebview to certain pages

2019-07-21 06:53发布

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 :)

3条回答
看我几分像从前
2楼-- · 2019-07-21 06:58

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;
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-07-21 07:04

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).

查看更多
Bombasti
4楼-- · 2019-07-21 07:17

Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:

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

UIWebViewDelegate Reference

查看更多
登录 后发表回答