所有,
我是XCode的新手,我试图在尝试使用WebView时如何最好地处理连接问题.我知道有关于SO的相关问题,但似乎都没有提供完整的解决方案.我有以下代码,但似乎有点低效.希望有人可以帮我重构它,使其可以在任何调用UIWebView的地方使用.
注意:请暂时忽略内存问题.我意识到必须加入.
- (void)viewDidLoad {
[webView setDelegate:self];
NSString *urlAddress = @"http://www.somesite.com/somepage";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
[super viewDidLoad];
}
// Check for URLConnection failure
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *connectionError = [[UIAlertView alloc] initWithTitle:@"Connection error" message:@"Error connecting to page. Please check your 3G and/or Wifi settings." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[connectionError show];
webView.hidden = true;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse …Run Code Online (Sandbox Code Playgroud)