开发者

locationServicesEnabled for iOS 3 and iOS 4

locationServicesEnabled changed from a property to a method.

This is deprecated:

CLLocationManager *manager = [[CLLocationManager alloc] 开发者_Python百科init];
if (manager.locationServicesEnabled == NO) {
     // ...
}

Now I should use:

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

I want to support iOS 3 and iOS 4 devices. How can I check this on iOS 3 devices and get rid of the deprecated warning?


Since the property 'locationServicesEnabled' is just deprecated, it is still available for use (for an undetermined amount of time). To handle the situation dynamically you need to provide a defensive solution. Similar to the solution above, I used:

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] ;
}

The call to 'instancesRespondToSelector' checks to see if the property is still available, then I double-check that the class itself supports the method call (being a static method it will report YES).

Just another option.


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


Try:

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

As a fix/work around.

Using the compiler defines will cause you issues when using the minimum deployment target to allow older OS versions access to your application.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜