【问题标题】:NSURLRequest cachePolicy and connection in UIWebViewUIWebView中的NSURLRequest cachePolicy和连接
【发布时间】:2012-08-24 01:07:15
【问题描述】:

我设置了一个 UIWebView,其 cachePolicy 为 NSURLRequestReloadIgnoringLocalAndRemoteCacheData,一切都很好:当有连接时,加载 UIWebView,当没有连接时,调用 connection:didFailWithError: 并得到我的 UIAlertView。

但是当我将 cachePolicy 更改为 NSURLRequestReturnCacheDataElseLoad (这是我真正想要的策略)时,在没有连接的情况下,缓存的页面会加载,然后,当我点击链接时......什么也没有发生。没有连接:didFailWithError:调用。没有 UIAlertView。

我该如何解决这个问题?

编辑:也许我有一个答案的开始......我至少可以使用以下方法确定何时单击 UIWebView 中的链接:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)req navigationType: (UIWebViewNavigationType)navigationType
{
    if (navigationType==UIWebViewNavigationTypeLinkClicked)
    {
        NSLog(@"Blah!");
    }
    return YES;
}

现在,代替 NSLogging "Blah!"我只需要让它调用connection:didFailWithError。

那么我该怎么做那个

【问题讨论】:

    标签: objective-c ios5 xcode4.4


    【解决方案1】:

    我做到了!

    我添加了这段代码:

    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)req navigationType:(UIWebViewNavigationType)navigationType
    {
        if (navigationType==UIWebViewNavigationTypeLinkClicked)
        {
            NSURL *scriptUrl = [NSURL URLWithString:@"http://www.homePageOfTheApp.com"];
            NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
            if (data == nil)
            {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error title." message:@"Error message." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
            }
        }
    return YES;
    

    }

    虽然我可能会用http://www.google.com 替换http://www.homePageOfTheApp.com",因为尽管我的主机有非常可靠的服务器,但我猜Google 的服务器会更好......

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2012-10-15
      • 1970-01-01
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多