我建议你使用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];
}
这对我来说工作得很好。
在加载页面时关闭网络,它会显示警报。 – iphonic
或者先关闭您的网络,然后运行您的应用程序。 – iPatel
我已经完成该操作,广告没有警报加载。 – dannysandler