【发布时间】:2015-04-27 23:31:36
【问题描述】:
我正在尝试用UIAlertView 处理特定的NSErrors,但是当调用webview didFailLoadWithError 时,没有调用if 语句中的任何代码。下面是我的条件语句:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
loadFailedBool = YES;
NSLog(@"Error code %ld", (long)[error code]);
if ([error code] != -999) {
//loading was cancelled and another one initiated. Don't show alert
return;
}
else if ([error code] == NSURLErrorTimedOut || [error code] == kCFURLErrorTimedOut) {
//connection timed out
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Network Connection timed out" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
alert.tag = 1;
}
else if ([error code] == -1005) {
//connection lost
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Network Connection to the host is lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
alert.tag = 1;
}
else if ([error code] == kCFURLErrorNotConnectedToInternet || [error code] == -1009) {
//no internet connection
NSLog(@"Error here");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Looks like you are not connected to the internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
alert.tag = 1;
}
else if ([error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 102) {
return;
}
}
我的问题是,我如何调用条件,因为有时在没有互联网的情况下日志会打印 -1009。谢谢
【问题讨论】:
-
说
[error code] == kCFURLErrorNotConnectedToInternet || [error code] == -1009是没有意义的,因为kCFURLErrorNotConnectedToInternet是-1009。
标签: ios error-handling conditional nserror