在我的 iOS 应用程序中,我有一个 mapview,我正在尝试使用
(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
方法来查看用户是否启用了定位服务以及是否有互联网连接。
在我的 .m 中,我正在这样做:
- (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总是启用的?还能是什么?
谢谢