如何在某个程序里面判定当前应用程序的定位服务是否可用,其实没有什么简单的方法。
这个[CLLocationManager locationServicesEnabled]检测的是整个iOS系统的位置服务开关,无法检测当前应用是否被关闭,只能通过CLLocationManagerDelegate的locationManager:didFailWithError:方法去检测:
- - (void)locationManager: (CLLocationManager *)manager
- didFailWithError: (NSError *)error {
- NSString *errorString;
- [manager stopUpdatingLocation];
- NSLog(@"Error: %@",[error localizedDescription]);
- switch([error code]) {
- case kCLErrorDenied:
- //Access denied by user
- errorString = @"Access to Location Services denied by user";
- //Do something...
- break;
- case kCLErrorLocationUnknown:
- //Probably temporary...
- errorString = @"Location data unavailable";
- //Do something else...
- break;
- default:
- errorString = @"An unknown error has occurred";
- break;
- }
- }
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
- [alert show];
- [alert release];
- }

本文介绍了一种检测iOS设备上定位服务是否可用的方法。利用CLLocationManagerDelegate的locationManager:didFailWithError:方法,可以检查定位服务的状态,包括用户是否授权、位置数据是否可用等。
3224

被折叠的 条评论
为什么被折叠?



