locationServicesEnabled已从属性更改为方法。
这已被弃用:
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
// ...
}现在我应该使用:
if (![CLLocationManager locationServicesEnabled]) {
// ...
}我想支持iOS 3和iOS 4设备。如何在iOS 3设备上检查这一点并摆脱已弃用的警告?
发布于 2010-10-22 08:59:46
尝试:
BOOL locationServicesEnabled;
CLLocationManager locationManager = [CLLocationManager new];
if( [locationManager respondsToSelector:@selector(locationServicesEnabled) ] )
{
locationServicesEnabled = [locationManager locationServicesEnabled];
}
else
{
locationServicesEnabled = locationManager.locationServicesEnabled;
}作为修复/变通方法。
当使用最小部署目标来允许旧版操作系统访问您的应用程序时,使用编译器定义将导致问题。
发布于 2011-03-30 22:06:29
由于属性'locationServicesEnabled‘刚刚被弃用,它仍然可以使用(在一段时间内)。为了动态地处理这种情况,您需要提供一个防御性的解决方案。与上面的解决方案类似,我使用:
BOOL locationAccessAllowed = NO ;
if( [CLLocationManager instancesRespondToSelector:@selector(locationServicesEnabled)] )
{
// iOS 3.x and earlier
locationAccessAllowed = locationManager.locationServicesEnabled ;
}
else if( [CLLocationManager respondsToSelector:@selector(locationServicesEnabled)] )
{
// iOS 4.x
locationAccessAllowed = [CLLocationManager locationServicesEnabled] ;
}对'instancesRespondToSelector‘的调用检查该属性是否仍然可用,然后我再次检查类本身是否支持该方法调用(作为一个静态方法,它将报告YES)。
只是另一种选择。
发布于 2010-10-04 20:35:30
编辑后的:
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_3_1
#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_3_2
if (![CLLocationManager locationServicesEnabled]) {
// ...
}
#else
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
// ...
}
#endif
#else
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
// ...
}
#endifhttps://stackoverflow.com/questions/3855152
复制相似问题