Jann
Jann

Reputation: 2213

Handling MKDirectionsRequest with iOS6 even when your app is compatible with iOS5

I want to make our app compatible with iOS6's MKDirectionsRequest (Transit Directions method). Apple states the best way to do this is:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    if ([MKDirectionsRequest isDirectionsRequestURL:url]){
        }
}

However, what is the best way to ensure this code is only run when the app is running in iOS6, and not iOS5? The app HAS to be compatible with iOS5 as well, but I MKDirectionsRequest is an iOS6.

I can't use compiler directives like:

#ifdefine iOS5

or anything.

Is this the best way?

BOOL atLeastIOS6 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0;

and then:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {

        if (atLeastIOS6)
        {
            if ([MKDirectionsRequest isDirectionsRequestURL:url]){
            }
        }
    }

I just want to make sure I don't crash on iOS5 when checking for openURLs

Upvotes: 0

Views: 1528

Answers (3)

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

Don't rely on iOS version. Check if the class exists by using NSClassFromString:

Class directionsRequestClass = NSClassFromString(@"MKDirectionsRequest");

if (directionsRequestClass)
    // it exists, so you can use it
else
    // doesn't exist, do something else

Upvotes: 4

Felix
Felix

Reputation: 35384

You can test if the class MKDirectionsRequest is available:

if ([MKDirectionsRequest class]) { 
 ... 
}

Upvotes: 2

smoothlandon
smoothlandon

Reputation: 78

I have seen numerous posts that discouraged using the system version string. It might have to do with trickiness converting 6.0.1 to a float, but not sure.

The preferred method seems to be to test a selector that appeared in a specific version. As such, I use these two methods to test for iOS 5 or 6 respectively.

+ (BOOL)isOS5Capable {

    return ( [UINavigationBar respondsToSelector:@selector(appearance)] );

}

+ (BOOL)isOS6Capable {

    return ( [UIView respondsToSelector:@selector(requiresConstraintBasedLayout)] );

}

If you look through the headers in the Apple frameworks, you will see that there are quite a few NS_AVAILABLE macros that specify what version the selector appeared. To create this function for iOS 6, I hunted around for a few minutes to find a static method to simplify the check (so I wouldn't have to allocate a class). By following this practice, you should be able to ensure that your app is version safe for future updates.

Upvotes: 1

Related Questions