[英]How to check if location services are enabled for a particular app prior to iOS 4.2?
如何检查用户是否允许mu app的位置? 通常我会使用CLLocationManager
类的authorizationStatus
方法,但它仅适用于iOS 4.2及更高版本。 是否有可能在使用SDK 4.2时以某种方式实现此目的,以便应用程序仍然可以在具有旧版iOS的设备上运行,或者我是否必须降级SDK? 沿着同一条线,我需要一个类似于iOS 4.0之前的locationServicesEnabled
方法的替代方案。
当您调用-startUpdatingLocation
,如果用户拒绝了位置服务,则位置管理器委托将接收对-locationManager:didFailWithError:
的调用,其中包含kCLErrorDenied
错误代码。 这适用于所有iOS版本。
我在下面的代码中结合了两种技术
MKUserLocation *userLocation = map.userLocation;
BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
BOOL locationAvailable = userLocation.location!=nil;
if (locationAllowed==NO) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
} else {
if (locationAvailable==NO)
[self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
}
嗯..我不认为使用authorizationStatus
或locationServicesEnabled
。 我做的是以下内容:
MKUserLocation *userLocation = mapView.userLocation;
if (!userLocation.location) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled"
message:@"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
}
我很高兴看到有更好的检查方法,但是如果我的应用程序被允许使用GPS,我的检测方式也没有任何问题。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.