为什么没有调用kCLErrorNetwork?

时间:2012-10-22 15:24:01

标签: iphone objective-c ios xcode

在我的iOS应用程序中,我有一个mapview,我正在尝试使用

(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

查看用户是否已启用位置服务以及是否存在互联网连接的方法。

在我的。我正在这样做:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        [_locationManager startUpdatingLocation];
    }
    return self;
}

和此:

- (void)locationManager:(CLLocationManager *)manager
   didFailWithError:(NSError *)error{


    [manager stopUpdatingLocation];
    NSLog(@"didFailWithError: %@\n\n\n", error);


    switch ([error code]) {
        case kCLErrorNetwork:{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"please check your internet connection or that you are not in airplane mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;

        case kCLErrorDenied:{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"enable location services to see your current position." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;

        default:{

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Unknown network error" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;
    }

}

如果用户不允许应用程序使用位置服务,他将正确弹出“kCLErrorDenied”。另一方面,如果我的电脑(我正在使用iPhone模拟器)的以太网电缆被删除,我没有得到kCLErrorNetwork错误。

可能是因为在iPhone模拟器上,即使没有互联网连接,也始终启用wifi? 还有什么呢?

由于

1 个答案:

答案 0 :(得分:0)

如果我打开飞行模式(并确保关闭Wi-Fi),我会在设备上看到类似的行为。我的猜测是它是一个SDK bug,所以我建议用Apple提交一个!如果我无法弄明白,我打算做更多的研究和提交错误。