paj*_*vic 36 iphone cllocationmanager ios-4.2
如何检查用户是否允许mu app的位置?通常我会使用该类的authorizationStatus方法CLLocationManager,但它仅适用于iOS 4.2及更高版本.是否有可能在使用SDK 4.2时以某种方式实现此目的,以便应用程序仍然可以在具有旧版iOS的设备上运行,或者我是否必须降级SDK?沿着同一条线,我需要一个类似于locationServicesEnablediOS 4.0之前的方法的替代方案.
Mar*_*don 60
当您拨打电话时-startUpdatingLocation,如果用户拒绝了位置服务,则位置管理员代表将接收-locationManager:didFailWithError:带有kCLErrorDenied错误代码的呼叫.这适用于所有iOS版本.
Eas*_*der 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];
}
Run Code Online (Sandbox Code Playgroud)
嗯..我没想过用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;
}
Run Code Online (Sandbox Code Playgroud)
我很高兴看到有更好的检查方法,但是如果我的应用程序被允许使用GPS,我的方式没有任何问题.
希望这可以帮助!