I’m working on an app with a UIWebView that goes to a single page — we’ll call it http://myPage.com — that has a few links on it. Displaying a UIAlertView for a connection error when the user first clicks the go-to-myPage button that leads to http://myPage.com is no problem. But if there’s no connection and the user clicks the go-to-myPage button, gets no UIAlertView because the cached version of http://myPage.com is returned, and then tries to follow a link from http://myPage.com, nothing happens. How can I rewrite or add to my code to display an alert in such a situation?
Here’s my code:
-(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];
}
}
And of course the go-to-myPage code simply calls -(void)loadWebViewWithbaseURLString:bus withURLString:us with the specific URLs as arguments.
I asked the question in a clearer (I hope) way at NSURLRequest cachePolicy and connection in UIWebView and was ultimately able to figure it out myself, so I posted the answer….