适用于iOS 3和iOS 4的locationServicesEnabled

时间:2010-10-04 12:22:00

标签: iphone objective-c cocoa-touch cllocationmanager

locationServicesEnabled已从属性更改为方法。

不推荐使用:

CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
     // ...
}

现在我应该使用:

if (![CLLocationManager locationServicesEnabled]) {
    // ...
}

我想支持iOS 3和iOS 4设备。如何在iOS 3设备上查看此信息并删除已弃用的警告?

3 个答案:

答案 0 :(得分:5)

由于属性'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)。

只是另一种选择。

答案 1 :(得分:2)

<强> Editted:

#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) {
     // ...
}
#endif

答案 2 :(得分:1)

尝试:

BOOL locationServicesEnabled;
CLLocationManager locationManager = [CLLocationManager new];
if( [locationManager respondsToSelector:@selector(locationServicesEnabled) ] )
{
    locationServicesEnabled = [locationManager locationServicesEnabled];
}
else
{
    locationServicesEnabled = locationManager.locationServicesEnabled;
}

作为修复/解决方法。

使用编译器定义将导致在使用最低部署目标时允许您使用较旧的OS版本访问应用程序时出现问题。