0

我正在开发一个应用程序,它带有一个UIWebView转到单个页面的应用程序——我们称之为它http://myPage.com——上面有几个链接。UIAlertView当用户第一次单击导致的 go-to-myPage 按钮时显示连接错误http://myPage.com是没有问题的。但是,如果没有连接并且用户单击 go-to-myPage 按钮,UIAlertView因为返回的缓存版本http://myPage.com返回 no,然后尝试跟随来自 的链接http://myPage.com,则没有任何反应。在这种情况下,如何重写或添加代码以显示警报?

这是我的代码:

-(void)loadWebViewWithbaseURLString:bus withURLString:us
{
self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:us] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 20];
self.connection=[[NSURLConnection alloc] initWithRequest:self.request delegate:nil];
NSError *error=nil;
NSURLResponse *response=nil;
if (self.connection)
{
    self.urlData = [ NSURLConnection sendSynchronousRequest: self.request returningResponse:&response error:&error];
    NSString *htmlString = [[NSString alloc] initWithData:self.urlData encoding:NSUTF8StringEncoding];
    [self.webV loadHTMLString:htmlString baseURL:[NSURL URLWithString:bus]];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] init];
    alert.title = @"Unfortunately, I seem to be having a hard time connecting to the Internet.  Would you mind trying again later?  I'll make it worth your while, I promise.";
    [alert show];
}
}

当然,go-to-myPage 代码只是-(void)loadWebViewWithbaseURLString:bus withURLString:us使用特定的 URL 作为参数进行调用。

4

1 回答 1

0

我在NSURLRequest cachePolicy 和 UIWebView 中的连接中以更清晰(我希望)的方式问了这个问题,最终我自己弄清楚了,所以我发布了答案......

于 2012-08-26T10:52:31.277 回答