0

他们有什么方法可以找到加载任何网址所花费的时间UIWebView吗?

实际上我的问题是,如果我的 url 需要超过10 分钟才能加载,则显示超时错误。

实际上我使用了一些代码来加载我的第一个 url,但是我的第一个加载 url 重定向到另一个 url,这需要太多时间来加载。

这是我用来加载我的第一个网址的代码。,

  NSURL *url = [NSURL URLWithString:urlAddress];
  NSURLRequest *requestObj=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
  [webView loadRequest:requestObj];

如何找到加载我的重定向网址所花费的时间?

4

3 回答 3

1

试试这个:

 @interface Foo: NSObject <UIWebViewDelegate> {
        NSDate *startDate;
    }

    // etc. In the implementation:
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
    webView.delegate = self;
    [webView loadRequest:requestObj];
    startDate = [[NSDate date] retain];

    - (void)webViewDidFinishLoading:(UIWebView *)webView
    {
        NSTimeInterval loadTimeInSeconds = [[NSDate date] timeIntervalSinceDate:startDate];
        // do what you want to it
    }
于 2012-07-28T08:19:30.520 回答
1

您可以在调用[webView loadRequest:requestObj];.. 时立即启动一个计时器,并在 - 的委托方法实现中停止该计时器,webViewDidFinishLoad: 并且超时间隔以秒为单位而不是以分钟为单位......

但是我认为您不应该这样做,因为所花费的时间并不总是恒定的,因为这取决于服务器响应的速度,这取决于特定时间实例的流量...

所以它可能会在 3 秒内完成加载,甚至可能需要 3 分钟.. 所以我的建议是给出大量的超时间隔,但也要显示一个网络连接指示器....希望这会有所帮助。

于 2012-07-28T08:21:50.407 回答
1

你可以这样做

  double timeOutTime = 60*10; //10 mins
  NSURL *url = [NSURL URLWithString:urlAddress];
  NSURLRequest *requestObj=[NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
  [self performSelector:@selector(timeOutMethod) withObject:nil afterDelay:timeOutTime];
  [webView loadRequest:requestObj];


- (void)timeOutMethod {
    ///do what ever you want to do
   }

这种方法可以取消上面的请求

- (void)webViewDidFinishLoading:(UIWebView *)webView {
        // This will cancel the previous request.
        [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(timeOutMethod) object:nil];
  }
于 2012-07-28T08:23:54.260 回答