简体   繁体   English

iOS UIWebView,如何阻止某些URL模式加载?

[英]iOS UIWebView, how to block a certain URL pattern from loading?

I have an iOS app with a web view controller, which is a delegate of a web view. 我有一个带有网络视图控制器的iOS应用,该控制器是网络视图的代表。 I would like my UIWebView to never show a url which has a certain keyword or part of a URL. 我希望UIWebView永远不要显示具有特定关键字或URL一部分的URL。 (In my case, I want to block a misleading error page from the app server, which can appear in response to any request). (就我而言,我想阻止来自应用服务器的误导性错误页面,该页面可能会响应任何请求而出现)。

I'm listening to these events: 我正在听这些事件:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {}
-(void)webViewDidFinishLoad:(UIWebView *)webView{}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {}

I'm not sure if listening to these events is enough to make sure that all internal page redirects, including those of javascript would be inspected and the offending url will be prevented from loading. 我不确定监听这些事件是否足以确保检查所有内部页面重定向,包括javascript的内部重定向,并且可以防止装入有问题的url。

How can I create a UIWebView which will never show a certain page? 如何创建一个不会显示特定页面的UIWebView?

You can check if the keyword is in the URL that was requested and stop the page from loading by returning false. 您可以检查关键字是否在请求的URL中,并通过返回false来阻止页面加载。 I am assuming the keyword you are looking for will be in the URL? 我假设您要查找的关键字将出现在URL中? Let's pretend it's errorPage . 我们假设它是errorPage

-(BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSURL *requestedURL = request.URL
    NSString *stringCheck = requestedURL.absoluteString;
// This is the important part--check if errorPage is in the URL.
    NSRange range = [urlString rangeOfString:@"errorPage"];
    if (range.location == NSNotFound) 
        return YES;
    else
        return NO;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM