2014-06-09 118 views
2

我正在用幾個UIWebView創建一個相當簡單的基於選項卡的應用程序。要通過App Store批准,如果沒有網絡連接,我需要執行錯誤。我是一名初學者,但我嘗試過,沒有發生任何事情。當UIWebView沒有互聯網連接時實現UIAlertView

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
               message:@"Can't connect. Please check your internet Connection" 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[alert show]; 

} 

我試過可達性,但其複雜,我不知道如何實現它到UIAlertView。這段代碼是否可以調整或者是垃圾?

+2

在加載頁面時關閉網絡,它會顯示警報。 – iphonic

+0

或者先關閉您的網絡,然後運行您的應用程序。 – iPatel

+0

我已經完成該操作,廣告沒有警報加載。 – dannysandler

回答

0

您可以通過以下鏈接查詢使用可達代碼在viewDidLoad中或viewWillAppear中..Download他們的網絡連接

https://github.com/tonymillion/Reachability

Reachability *reach = [Reachability reachabilityForInternetConnection]; 
    NetworkStatus netStatus = [reach currentReachabilityStatus]; 
    if (netStatus != NotReachable) 
    { 
    NSLog(@"Async.....Network is Available"); 
    } 
    else 
    { 
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Network Error" message:@"No Network Available" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil , nil]; 
     [alertView show]; 
    } 

或者您可以將提醒下面的UIWebView

    的委託方法
  • (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

希望它可以幫助你......!

+0

這隻適用於WebView,但在WebView處於Tab中時不起作用。 – dannysandler

+0

它會工作...只需將您的視圖控制器的ViewWillAppear上面的代碼加載webview ..! – Vidhyanand

+0

我試過了: - (void)viewWillAppear:(BOOL)animated {} {} {}}可達性*達到...等等,但它不起作用... – dannysandler

1

您爲UIWebView指派了委託的視圖控制器; (在你設置了webview.delegate = self的viewcontroller的代碼中),你需要在頭文件(.h)中提交UIWebViewDelegate協議。只有這樣,你的方法:

- (空)webView的:(UIWebView的*)webView的didFailLoadWithError:(NSError *)錯誤

將被觸發。我想你錯過了這個,否則你的過程是正確的。 因此,在您的.h文件中添加UIWebViewDelegate。

希望這會有所幫助。

2

我建議你使用delegate methods of webView並在ViewController中實現它。

webView主要有三種委託方法。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 

- (void)webViewDidFinishLoad:(UIWebView *)webView 

在didFailLoadWithError方法中,您可以使用[錯誤代碼]標識錯誤代碼。您可以根據您的要求使用其中的任何一種。

// Error codes for CFURLConnection and CFURLProtocol 
    kCFURLErrorUnknown = -998, 
    kCFURLErrorCancelled = -999, 
    kCFURLErrorBadURL = -1000, 
    kCFURLErrorTimedOut = -1001, 
    kCFURLErrorUnsupportedURL = -1002, 
    kCFURLErrorCannotFindHost = -1003, 
    kCFURLErrorCannotConnectToHost = -1004, 
    kCFURLErrorNetworkConnectionLost = -1005, 
    kCFURLErrorDNSLookupFailed = -1006, 
    kCFURLErrorHTTPTooManyRedirects = -1007, 
    kCFURLErrorResourceUnavailable = -1008, 
    kCFURLErrorNotConnectedToInternet = -1009, 

這些錯誤代碼在CFNetworkError.h中可用。在我的應用程序中,我以這種方式使用。

if ([error code] == kCFURLErrorNotConnectedToInternet) { 
     // if we can identify the error, we can present a more precise message to the user. 
     UIAlertView *alertView=[[[UIAlertView alloc] initWithTitle:APP_NAME message:@"Sorry, there doesn’t seem to be an internet connection." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]autorelease]; 
     [alertView show]; 
} 

這對我來說工作得很好。

+0

感謝提供此代碼列表;它會幫助我解決我想要採取的方法。多謝! – Lev