Iphone 如何检查iOS 4.2之前的特定应用程序是否启用了位置服务?
如何检查用户是否允许mu应用的位置? 通常我会使用Iphone 如何检查iOS 4.2之前的特定应用程序是否启用了位置服务?,iphone,cllocationmanager,ios-4.2,Iphone,Cllocationmanager,Ios 4.2,如何检查用户是否允许mu应用的位置? 通常我会使用CLLocationManager类的authorizationStatus方法,但它仅在iOS 4.2及更新版本中可用。在仍然使用SDK 4.2的情况下,是否有可能以某种方式实现这一点,从而使应用程序仍然可以在具有较旧版本iOS的设备上运行,或者我必须降级SDK? 同样,在iOS 4.0之前的locationServicesEnabled方法中,我需要一个类似的替代方法。。我没有想到使用authorizationStatus或locationS
CLLocationManager
类的authorizationStatus
方法,但它仅在iOS 4.2及更新版本中可用。在仍然使用SDK 4.2的情况下,是否有可能以某种方式实现这一点,从而使应用程序仍然可以在具有较旧版本iOS的设备上运行,或者我必须降级SDK?
同样,在iOS 4.0之前的
locationServicesEnabled
方法中,我需要一个类似的替代方法。。我没有想到使用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方面没有任何问题
希望这有帮助 当您调用
-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];
}
我更喜欢调用
[CLLocationManager locationServicesEnabled]
,如下所述。这样可以防止不必要的呼叫。对于在这里遇到问题而没有“Previor iOS 4.2”约束的用户,答案是:[CLLocationManager authorizationStatus]@SpacyRicochet这是否回答了问题?OP询问如何确定是否为特定应用程序启用了位置服务,而[CLLocationManager locationServicesEnabled]
只是测试设备范围的位置服务<代码>[CLLocationManager authorizationStatus]测试特定于应用程序的授权。@SpacyRicochet[CLLocationManager locationServicesEnabled]仅检查常规“位置服务”开关,它不检查单个应用程序的状态。@现在它是kCLAuthorizationStatusDenied
请注意,即使启用了位置服务,用户位置也可能不可用。例如,用户可能不在所有传感器的覆盖范围内(例如,未打开Wi-Fi的iPod touch)。与上述问题相同。请不要假设nil用户位置对应于no authorization,因为这可能不是真的。我认为这不太正确。[CLLocationManager locationServicesEnabled]告诉您所有应用的位置都处于打开或关闭状态,[CLLocationManager authorizationStatus]告诉您应用是否有权检查位置。所以UIAlertView应该说打开位置,而不是授权应用程序。但它很有用,谢谢@EasyCoder