我建議你使用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