如何检查用户是否允许 mu 应用程序的位置?通常我会使用类authorizationStatus
的方法CLLocationManager
,但它只在 iOS 4.2 和更新版本中可用。是否有可能在仍然使用 SDK 4.2 的同时以某种方式实现这一点,以便该应用程序仍然可以在具有旧版本 iOS 的设备上运行,还是我必须降级 SDK?locationServicesEnabled
同样,对于 iOS 4.0 之前的方法,我需要一个类似的替代方案。
问问题
59415 次
3 回答
60
当您调用 时-startUpdatingLocation
,如果用户拒绝位置服务,位置管理器委托将收到-locationManager:didFailWithError:
带有kCLErrorDenied
错误代码的调用。这适用于所有版本的 iOS。
于 2011-01-15T22:13:39.453 回答
37
我在下面的代码中结合了两种技术
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];
}
于 2011-06-12T15:46:06.540 回答
7
嗯..我没想过使用authorizationStatus
or 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 的方式没有任何问题。
希望这可以帮助!
于 2011-01-15T21:16:03.627 回答