0

目前我仍在阅读一些文档和教程NSTimer。根据我目前的理解,我们调用计时器并给它一个方法,以便它会重复自己。然后我想到了一个想法。(我正在开发其他应用程序项目)

我打算做什么

  • 实施UIWebView
  • 使用检查当前 url(绝对字符串)的方法在 中实现NSTimer(可能是 0.5 秒),如果它不是我指定的,它将重定向 url。UIWebViewUIWebView

我有我想要实现的代码,目的是纠正我的知识,UIWebViewNSTimer在我开始工作之前看看它是否合理。(我仍在阅读文档。所以想了解更多并尝试更多在工作之前)

现在让我感到困惑的是,我的方法是否兼容NSTimerNSTimer在方法中使用是否合理UIWebView viewDidLoad?由于应用程序每 0.5 秒不断运行该方法,是否会出现内存过载/崩溃?

例如NSTimer(如果我错了,请纠正我。但由于我希望它永远运行,只要用户在其中UIWebView,我将只需要此代码集而不包括NSRunLoop

NSTimer *t = [NSTimer scheduledTimerWithTimeInterval: 2.0
                      target: self
                      selector:@selector(onTick:)
                      userInfo: nil repeats:NO];

-(void)onTick:(NSTimer *)timer {
   //do smth
}

EDIT2-@Robert Ryan,这是我正在使用的代码。(当我第一次更改此代码时,它刚刚工作)**(如果我屏蔽了 range2 部分,那么错误域 error-999 停止了。但视图仍然不加载)

     - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString];
    NSRange range1= [currentURL rangeOfString:@"news"];
    NSRange range2= [currentURL rangeOfString:@"pdf"];
    if (range1.location == NSNotFound)
    {
        NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];

        return NO; //no to this if its not found.
    }
    if(range2.location==NSNotFound)
    {
        NSURL * url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];
        return NO; //no to this if its not found
    }

    return YES; //everything else ok

}

EDIT3 - 如果我将 NSRange range1 和 range2 组合成 1 个语句,那么它会再次工作(希望它在一个小时后仍然可以工作。xD)

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString];
    NSRange range1= [currentURL rangeOfString:@"news"];
    NSRange range2= [currentURL rangeOfString:@"pdf"];
    if (range1.location == NSNotFound & range2.location==NSNotFound)
    {
        NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
        [webView loadRequest:[NSURLRequest requestWithURL:url]];

        return NO;
    }
return YES;
}
4

2 回答 2

2

如果您想要确保用户不会导航到其他网址,您可以添加以下内容

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[request.URL absoluteString] isEqualToString:@"http://YourAllowableURL1.com"]) {
        return YES;
    }

    if ([[request.URL absoluteString] isEqualToString:@"http://YourAllowableURL2.com"]) {
        return YES;
    }

    return NO;// user will not be able to go to any other urls
}

您还可以指定允许的 URL 列表,并确保仅对这些 URL 返回 YES

于 2012-06-27T06:40:13.270 回答
1

使用 NSTimer 来确定是否尝试访问“未经批准”的网站并不是您想要的。shouldStartLoadWithRequest如果 URL 不可接受,这将允许您取消当前请求。但是,它不允许您更改 URL 以实现重定向。如果你真的想重定向,你可以发起一个全新的请求,也许在一点延迟之后,例如,

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString];
    NSRange range = [currentURL rangeOfString:@"imc.jhmi.edu"];

    if (range.location == NSNotFound)
    {
        double delayInSeconds = 0.1;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            NSURL *url = [NSURL URLWithString:@"http://www.imc.jhmi.edu/news.html"];
            [webView loadRequest:[NSURLRequest requestWithURL:url]];
        });

        return NO;
    }

    return YES;
}

实际上看起来您不必等待,您实际上可以立即发起新请求,但在前一个请求shouldStartLoadWithRequest完成之前这样做似乎不正确。但如果你想这样做,那显然是:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *currentURL = [[request URL] absoluteString];
    NSRange range = [currentURL rangeOfString:@"imc.jhmi.edu"];

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

        return NO;
    }

    return YES;
}

就个人而言,我根本不会重定向用户,但最多会弹出一个 UIAlertView 告诉他们必须留在这个网站上,但这取决于你:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSLog(@"%s %@", __FUNCTION__, request);

    NSString *currentURL = [[request URL] absoluteString];
    NSRange range = [currentURL rangeOfString:@"imc.jhmi.edu"];

    if (range.location == NSNotFound)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"This app only follows links within the www.imc.jhmi.edu site." 
                                                        message:nil 
                                                       delegate:nil 
                                              cancelButtonTitle:@"Ok" 
                                              otherButtonTitles:nil];
        [alert show];
        // [alert release]; // use in non-ARC
        return NO;
    }

    return YES;
}
于 2012-06-27T13:28:04.620 回答