Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c NSTimer与UIWebView-可行性_Objective C_Ios_Uiwebview_Nstimer - Fatal编程技术网

Objective c NSTimer与UIWebView-可行性

Objective c NSTimer与UIWebView-可行性,objective-c,ios,uiwebview,nstimer,Objective C,Ios,Uiwebview,Nstimer,目前我还在阅读NSTimer的一些文档和教程。根据我目前的理解,我们调用计时器并给它一个方法,这样它就会重复自己。然后我想到了一个主意。(我正在开发另一个应用程序项目) 我打算做什么 实现UIWebView 在UIWebView中使用检查UIWebView当前url(绝对字符串)的方法实现NSTimer(可能0.5秒),如果它不是我指定的,它将重定向url 我有我想要实现的代码,目的是纠正我在UIWebView和NSTimer上的知识,并在开始工作之前看看它是否合理。(我仍在阅读文档。因此希

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

我打算做什么

  • 实现
    UIWebView
  • UIWebView
    中使用检查
    UIWebView
    当前url(绝对字符串)的方法实现
    NSTimer
    (可能0.5秒),如果它不是我指定的,它将重定向url
我有我想要实现的代码,目的是纠正我在
UIWebView
NSTimer
上的知识,并在开始工作之前看看它是否合理。(我仍在阅读文档。因此希望在开始工作之前了解更多并尝试更多)

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

e、 g的
NSTimer
(如果我错了,请纠正我。但是,由于我希望只要用户在
UIWebView
中,它就会一直运行,因此我只需要此代码集,而不包括
nsrunlop

EDIT2-@Robert Ryan,这是我正在使用的代码。(刚才我第一次更改为该代码时,该代码起作用)**(如果我屏蔽了range2部分,则错误域error-999停止。但视图仍然没有加载)

EDIT3-如果我将NSRange range1和range2合并为1语句,则它将再次工作(希望在一小时后仍能工作。xD)


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

- (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返回“是”

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

- (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(是)

使用NSTimer来确定是否尝试访问“未经批准”的网站并不是您想要的。如果URL不可接受,
shouldStartLoadWithRequest
将允许您取消当前请求。但是,它不允许您更改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;
}

使用NSTimer来确定是否尝试访问“未经批准”的网站并不是您想要的。如果URL不可接受,
shouldStartLoadWithRequest
将允许您取消当前请求。但是,它不允许您更改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;
}

可能并不矛盾,但似乎效率低下。你为什么要这么做?你想解决什么问题?您是否担心用户会跟随您页面上的链接,从而将他们从相关url中删除?或者你害怕网页本身会重定向吗?无论如何,与使用NSTimer(或将来使用任何其他方法触发方法)相比,使用UIWebViewDelegate方法不是更好吗?请检查这些线程-,@RobertRyan-我尝试了
webView:shouldStartLoadWithRequest:navigationType:
,但根据其他ppl告诉我的,这只是开始(我假设加载页面后它不起作用)。我想解决的是,当人们在视图中停留一段时间(比方说5分钟),他们决定进入其他url时,我想限制他们进入我指定的url之外的其他url。不,这不仅仅是为了初始加载,而是为了通过UIWebView进行的所有交互。它的设计正是为了你所描述的。比使用NSTimer或其他延迟方法调用进行轮询要好得多。尝试
webView:shouldStartLoadWithRequest:navigationType
out。我想你会高兴的。请确保您设置了webview的委托(否则您可能无法收到消息)。我已设置了委托。我目前正在使用这种方法,但对我来说不起作用。我已经发布了我目前使用的这个方法的代码。如果我启用/禁用[webView reload],它只会进行回击,要么加载所有内容,要么不加载任何内容。可能不是不兼容的,但似乎效率低下。你为什么要这么做?你想解决什么问题?您是否担心用户会跟随您页面上的链接,从而将他们从相关url中删除?或者你害怕网页本身会重定向吗?无论如何,与使用NSTimer(或将来使用任何其他方法触发方法)相比,使用UIWebViewDelegate方法
webViewDidFinishLoad:
或<