简体   繁体   English

我们可以限制某些网站在网络视图中打开吗

[英]Can we restrict some websites to open in web view

I want in my app to not to open some particular sites in web view. 我希望在我的应用中不要在Web视图中打开某些特定的网站。 So can we restrict any website in such a way. 因此,我们可以通过这种方式限制任何网站吗? I want to add one more thing that i want to completely restrict a website not any particular page. 我想补充一点,我想完全限制网站,而不是任何特定页面。 Thanks in advance. 提前致谢。

Yes, you can block the page from loading in this UIWebview delegate method. 是的,您可以使用此UIWebview委托方法阻止页面加载。 Remember to set the class as the webviews delegate. 记住将类设置为webviews委托。 webView.delegate = self; , and the class needs to be UIViewController <UIWebViewDelegate> ,并且该类必须是UIViewController <UIWebViewDelegate>

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.absoluteString rangeOfString:@"google"].length != 0) {
        // maybe show a UIAlertView to indicate the page is blocked?
        return NO;  
    }else {
        return YES;
    }
}

EDIT: now it checks if google is mentioned in the URL, and blocks it if it contains Google 编辑:现在,它检查URL中是否提到了google,并阻止它包含Google的信息

A little improvement : save the websites you want to block in a NSArray : 一点改进:将要阻止的网站保存在NSArray中:

NSArray *restrictedSites = [NSArray arrayWithObjects:@"http://www.google.com", @"http://www.google.co.uk", nil];

Now check if the website requested is one of the restricted websites : 现在检查所请求的网站是否为受限网站之一:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *site = request.URL.absoluteString

    for(NSString *str in restrictedSites)
    {
        if ([site isEqualToString:str]) 
        {
            return NO;
        }
    }
    return YES;
}

Alternatively you can check if the website requested contains a URL. 或者,您可以检查所请求的网站是否包含URL。 In th following case, all the url containing "http://www.google" or "http://www.apple" will be blocked : 在以下情况下,所有包含“ http://www.google”或“ http://www.apple”的网址都将被阻止:

First create the restricted site array : 首先创建受限站点数组:

NSArray *restrictedSites = [NSArray arrayWithObjects:@"http://www.google", @"http://www.apple", nil]; 

Now check if the website requested contains one of the restricted websites (without .com or .co.uk etc...) : 现在检查所请求的网站是否包含受限网站之一(没有.com或.co.uk等):

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *site = request.URL.absoluteString

    for(NSString *str in restrictedSites)
    {
        if ([site rangeOfString:str].location != NSNotFound) 
        {
            return NO;
        }
    }
    return YES;
}

So here, all http://www.google.com , http://www.google.fr , http://www.google.co.uk will be blocked. 因此,此处所有http : //www.google.com,http : //www.google.fr,http://www.google.co.uk都将被阻止。

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

相关问题 我们可以使用 UI WEB VIEW iOS 进行交易吗 - Can we do transaction using UI WEB VIEW iOS 我想限制某些视图控制器以在ios6中进行横向显示 - I want to restrict some of the view controller to landscape in ios6 如何在 iPhone 上以网页视图打开网页 - How to open web page in web view on iPhone 我们可以在webview上添加UI按钮,uilabels,并将它们与iPhone中的Web视图一起滚动吗? - Can we add UIbuttons, uilabels on to webview and make them scrollable along with the web view in iPhone? 如何从网络加载图像,并在获得一些数据后立即在视图中显示它 - How can I load the image from web, and show it in the view as soon as I get some data 我们可以在iPhone应用程序中打开PowerPoint文件吗? - can we open a PowerPoint file in an iPhone application? 如何在UI Web View中打开UITextView URL? - How to open a UITextView URL in UI Web View? 在标签栏应用程序中,我们只想在iPhone中以横向查看某些视图,以纵向查看某些视图 - In tabbar application we want to view only some view to landscape and some to portrait in iphone 当我们打开UIPickerView时,视图将如何自动滚动? - How will the view be auto scroll when we open UIPickerView? 我们怎样才能解除我们不再使用它的观点? - How can we dealloc the view that we are no longer use it any more?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM