Restrict UIWebview to certain pages

夙愿已清 提交于 2019-12-24 00:31:47

问题


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


回答1:


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;
}



回答2:


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




回答3:


Use UIWebViewDelegate method webView:shouldStartLoadWithRequest:navigationType:

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

UIWebViewDelegate Reference



来源:https://stackoverflow.com/questions/7673116/restrict-uiwebview-to-certain-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!