iPhoneアプリのHTTP通信時のエラーハンドリング
下記2つのHTTP通信での、それぞれのエラー時のハンドリングについてmemoしておきます。
・NSURLConnectionでNSData型を取得する場合
・UIWebViewのloadRequestでリクエスト送ってweb画面を表示する場合
どちらの場合も、エラーが起きたらアラートビューを出して別画面へ戻すようにしました。
・NSURLConnectionでNSData型を取得する場合
- (void)viewDidLoad { [super viewDidLoad]; [self configureView]; } - (void)configureView { NSString *url = @"http://192.168.1.1:8080/app/select_question_id"; /* POST */ NSString *keyValue = [NSString stringWithFormat:@"grade=%@&category=%@&level=%@&personalId=%@", self.grade, self.category, self.level, self.personalId]; NSData *post = [keyValue dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:post]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // HTTPリクエストのエラー if (response == nil){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"問題の読み込みに失敗しました", @"") message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil]; [alert show]; return; } self.questionIds = [NSJSONSerialization JSONObjectWithData:response options:0 error:nil]; // JSONデータのパースエラー if (self.questionIds == nil){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"問題の読み込みに失敗しました", @"") message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil]; [alert show]; return; } } // delegateメソッド - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { [self goSetting]; // 設定画面へ飛ばす } } // 設定画面へ飛ばす - (void)goSetting { SettingTableViewController *settingTableViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"settingTableViewController"]; settingTableViewController.navigationItem.hidesBackButton = YES; // 戻るボタン消しとく [self.navigationController pushViewController:settingTableViewController animated:YES]; }
・UIWebViewのloadRequestでリクエスト送ってweb画面を表示する場合
- (void)configureView { NSString *url = @"http://192.168.1.1:8080/ichiyazuke_web/select_question_by_id"; /* POST */ NSString *keyValue = [@"questionId=" stringByAppendingString:self.questionId]; NSData *post = [keyValue dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:url]]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:post]; CGRect frame = [[UIScreen mainScreen] bounds]; self.myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,frame.size.width,frame.size.height)]; self.myWebView.scalesPageToFit = YES; self.myWebView.delegate = self; [self.view addSubview:self.myWebView]; [self.myWebView loadRequest:request]; } // delegateメソッド // webViewが画面のloadに失敗したら呼ばれる - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"問題の読み込みに失敗しました", @"") message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil]; [alert show]; return; } // delegateメソッド - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { [self.navigationController popViewControllerAnimated:YES]; // 前画面に戻る } }