2010-10-04 51 views
2

locationServicesEnabled从属性更改为方法。locationServicesEnabled适用于iOS 3和iOS 4

这是弃用:

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

现在我应该使用:

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

我想支持的iOS 3和iOS 4设备。我如何在iOS 3设备上检查这一点并摆脱已弃用的警告?

回答

1

尝试:

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

作为固定/变通。

使用编译器定义会导致您在使用最低部署目标时允许较早的操作系统版本访问您的应用程序时出现问题。

+0

我试过你的方法,但无论一般位置服务是否启用,'locationServicesEnabled'总是YES。我检查了另一种方法,他从来没有进入这里'#if __IPHONE_OS_VERSION_MIN_REQUIRED> __IPHONE_3_1'(在iOS 4.1的iPod Touch 2G上测试过)看起来您的最低部署目标是正确的。 – testing 2010-10-27 13:24:19

+0

现在我使用iPad进行了测试,但iPad也触及了方法而不是属性。 – testing 2010-10-27 13:52:10

+0

基本SDK为4.1,部署目标为3.0 – testing 2010-10-27 14:04:20

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 
+0

谢谢。为什么选择iOS 3.1? – testing 2010-10-04 13:21:20

+0

啊,因为当我建立我的应用程序时,我们通常使用3.1.3,这意味着我们可以识别常量__IPHONE_3_1,但是因为3.1.3将等于3.1,因此它将转到else分支。原因也是3.1.3是4.0之前最高的iphone sdk(3.2仅适用于ipad) – vodkhang 2010-10-04 13:31:42

+0

如果应用程序可以在iPad上运行,该怎么办? – testing 2010-10-04 13:37:18

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)。

只是另一种选择。

相关问题