XCode webview连接/服务器错误处理

Dav*_*ave 0 iphone xcode uiwebview nsurlconnection

所有,

我是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 *)response;

    //Check for server error
    if ([httpResponse statusCode] >= 400) {
        UIAlertView *serverError = [[UIAlertView alloc] initWithTitle:@"Server error" message:@"Error connecting to page.  If error persists, please contact support." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
        [serverError show];
        webView.hidden = true;

    //Otherwise load webView
    } else {
        // Redundant code
        NSString *urlAddress = @"http://somesite.com/somepage";
        NSURL *url = [NSURL URLWithString:urlAddress];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

        [webView loadRequest:urlRequest];
        webView.hidden = false;
    }
}

// Seems redundant since we are already checking the URLConnection
- (void)webView:(UIWebView *)webView didFailLoadWithError:(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];
}
Run Code Online (Sandbox Code Playgroud)

我想我想知道的是,是否有任何捷径可以实现所需的功能?我可以通过WebView以某种方式直接访问URLResponse吗?URLConnection或UIWebView的nil值是否意味着连接错误而不必显式检查它们?是否有更简单的方法将URLRequest传递给委托方法,因此它不会被重新创建两次?

提前致谢!

Bar*_*oon 5

- (void)viewDidLoad {
    webView = [[UIWebView alloc]init];
    [webView setDelegate:self];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourUrl.com"]]]
    [super viewDidLoad];
}

#pragma mark UIWebView delegate methods
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //read your request here
    //before the webview will load your request
    return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
    //access your request
    webView.request;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    //access your request 
    webView.request;    
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"could not load the website caused by error: %@", error);
}
-(void)dealloc{
[webView release];
[super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

您可以将带有NSURLRequest的网页直接加载到您的网页浏览中.使用委托回调方法,您可以更改Webview的行为.

有了这段代码,你应该完成它.