iOS UIWebView - 限制 HTML 路径 [英] iOS UIWebView - restricting HTML path

查看:25
本文介绍了iOS UIWebView - 限制 HTML 路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIWebview,它正在加载特定的网站部分(新闻).我想阻止用户访问网站的其他部分,这意味着他们应该只呆在新闻部分/阅读 pdf 文章.

I have an UIWebview and it's loading a particular website section (news). And I want to block the users to access other part of the site, meaning they should only stay in the news section / reading the pdf articles.

我已经构建了一组代码,但遗憾的是,由于某些技术限制,目前还无法对其进行测试.下周才能访问.但是,我认为现在是在这里提问并让高级用户查看我的草稿代码并看看它是否可行的好时机.

I've constructed a set of code, but sadly unable to test them out just yet due to some technical constraint. Will only be able to access it next week. However, I thought it will be a good time to ask here and let the advance user view my draft code and see if its feasible.

- (void)viewDidLoad
{   
    [super viewDidLoad];

    NSString *urlAddress = @"http://www.imc.jhmi.edu/news.html";
    NSURL *url =[NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    //finding current url and lead it back if it strayed off
    NSString *currentURL = webView.request.URL.absoluteString;
    NSRange range1 = [currentURL rangeOfString:@"news"];
    NSRange range2 = [currentURL rangeOfString:@"pdf"];

我应该使用

    if (range1.location ==NSNotFound){
    currentURL = @"http://www.imc.jhmi.edu/news.html";
    [webView reload];
    }else if (range2.location ==NSNotFound){
    currentURL = @"http://www.imc.jhmi.edu/news.html";
    [webView reload];
    }

}

还是这套代码?

    if (range1.location ==NSNotFound){
    currentURL = @"http://www.imc.jhmi.edu/news.html";
    url = [NSURL URLWithString:currentURL
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    }else if (range2.location ==NSNotFound){
    currentURL = @"http://www.imc.jhmi.edu/news.html";
    url = [NSURL URLWithString:currentURL
    [webView loadRequest:[NSURLRequest requestWithURL:url]];
    }

代码有问题吗?如果是,请指出为什么它不起作用,因为我仍处于学习阶段.在这个阶段将是一个学习更多的好机会.我正在寻找的逻辑是它将加载初始新闻部分.UIWebView 应该只显示那里的新闻部分/PDF 文章.如果用户尝试转到其他路径,它应该检测当前 url 并将它们重定向回新闻部分.

And is there any problem with the code? if yes, please point out why it wouldn't work as I'm still in learning phase. Would be a great opportunity to learn more at this stage. The logic I'm looking for is it will load the initial news section. UIWebView should only display the news section / PDF articles from there. If the user try to go to other path, it should detect the current url and redirect them back to news section.

推荐答案

要限制某些 url 的负载,请执行以下操作

To restrict the load of certain url do the following

webView.delegate = self;

并添加如下函数

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
     NSString *urlString = [[request URL]absoluteString];
     if(ulrString == @"restricted ur")
          return NO;   //Restrict
     else
          return YES;  //Allow
}

这篇关于iOS UIWebView - 限制 HTML 路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆