[英]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.