2015-05-27 140 views
1

我在遊戲中使用UIWebView來顯示客戶支持表單。我注意到,如果服務器返回503錯誤(從負載平衡器發送的http狀態碼),我有問題關閉webviewUIWebView從加載頁面獲取http狀態代碼

有沒有辦法輕鬆獲取此http狀態?我可以做一些設置,以便在http狀態碼不是200時調用didFailWithError

加載代碼

UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.frame]; 

NSURL *nsURL = [NSURL URLWithString:url]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:nsURL]; 

webView.opaque = YES; 

webView.userInteractionEnabled = YES; 

[webView loadRequest:request]; 

webView.delegate = self; 

closableWebViewController = [[UIViewController alloc] init]; 

[closableWebViewController.view setFrame:self.view.frame]; 

[closableWebViewController.view addSubview:webView]; 

[self presentViewController:closableWebViewController animated:YES completion:nil]; 

此委託方法是不是叫

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 
{ 
NSLog(@"webview error %@",error); 

if (closableWebViewController) 
{ 
    NSLog(@"closing webview"); 
    [closableWebViewController dismissViewControllerAnimated:TRUE completion:^{ 
     closableWebViewController = nil; 
     NSLog(@"webView closed"); 
    }]; 

} 
} 

回答

1

你不能很容易地得到它,但有一個很好的技巧,很好地記錄在這裏:

How to detect and handle HTTP error codes in UIWebView?

+0

是的,我檢查了這個頁面,但是在我的情況下找到獲取調用didFailWithError委託的方法。我可以讓我的系統管理員改變負載均衡器發送錯誤的方式。也許如果我從錯誤頁面中刪除所有的HTML輸出,它會解決這個問題。 –

+0

是的,這將解決它。另一種選擇是讓你使用NSUrlConnection,並在成功時手動填充UIView。 – Mika

+0

我終於使用了另一個技巧(通過JavaScript獲取html內容),但是這些信息在ios SDK中顯然缺失。 –