2014-06-09 73 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