XCode webview连接/服务器错误处理 [英] XCode webview connection/server error handling

查看:98
本文介绍了XCode webview连接/服务器错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,

我对XCode相当新,并试图在尝试使用WebView时如何最好地处理连接问题。我知道有关于SO的相关问题,但似乎没有提供完整的解决方案。我有以下代码,但似乎有点低效。希望有人可以帮我重构它,使其可以在任何调用UIWebView的地方使用。

I am fairly new to XCode and am trying to get a handle on how to best deal with connection issues when trying to use a WebView. I know there are related questions on SO, but none seem to offer complete solutions. I have the following code, but it seems a little inefficient. Hopefully, someone can help me refactor it down to a point where it can be usable anywhere that a UIWebView is called.

注意:请暂时忽略内存问题。我意识到还必须添加。

NOTE: Please ignore memory issues for now. I realize that has to be added as well.

- (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];
}

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

I guess what I'm wondering is, are there any shortcuts to achieve the desired functionality? Can I somehow access the URLResponse via the WebView directly? Does a nil value for the URLConnection or UIWebView imply connection errors without having to explicitly check for them? Is there an easier way to pass the URLRequest down into the delegate methods so it doesn't have be recreated twice?

提前致谢!

推荐答案

- (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];
}

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

You can load the webpage with NSURLRequest directly into your webview. With the delegate callback methods you can change the behavior of your webview.

使用这段代码,您应该完成它。

With this piece of code you should get it done.

这篇关于XCode webview连接/服务器错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆