So I'm using NSClassFromString to check if the user has iOS 5 or iOS 6 installed to use Apple's new iOS 6 MKMapItem. This is the code:

- (void)openDirections:(id)sender {
    Class mapClass = NSClassFromString(@"MKMapItem"); 

    if (mapClass == nil) {
    // iOS 5, do something here
    }
    else {
    // iOS 6, open up maps with MKMapItem.
    }
}

By the code above, when I run it on iOS 5.1 simulator or a iOS 5.1 device, the iOS 6 branch gets run. However, if I use

Class mapClass = NSClassFromString(@"PKPass");

which was also introduced in iOS 6, my code follows the appropriate iOS 5 or iOS 6 branch. Am I missing something? Thanks.

有帮助吗?

解决方案

MKMapItem exists in prior versions of iOS because it was in development then, therefore the class existed. Instead, Apple recommends that you should use the following code also checking for a method they specifically added in iOS 6.0:

Class itemClass = [MKMapItem class];
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) {
   // Use class
}

Information from MKMapItem Class Reference

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top