小编Dav*_*ave的帖子

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

所有,

我是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)

iphone xcode uiwebview nsurlconnection

0
推荐指数
1
解决办法
1万
查看次数

标签 统计

iphone ×1

nsurlconnection ×1

uiwebview ×1

xcode ×1