2012-12-05 58 views
0

我正在构建一个使用Mapkit的应用程序。我知道这只在IOS6中可用。 所以我应该检查这是否可用。我正在使用下面的代码。如何检查Mapkit是否可用?

if(NSClassFromString(@"MKMapKit")) { 
     // MKMapKit is available in this OS 
     CLLocationCoordinate2D coords = 
     CLLocationCoordinate2DMake(51.097185,5.621653); 

     NSDictionary *address = @{ 
     (NSString *)kABPersonAddressStreetKey: @"Weg naar oqdffds 59", 
     (NSString *)kABPersonAddressCityKey: @"Msfsf", 
     (NSString *)kABPersonAddressStateKey: @"Limbusqfqsdf", 
     (NSString *)kABPersonAddressZIPKey: @"3670", 
     (NSString *)kABPersonAddressCountryCodeKey: @"BE", 
     (NSString *)kABPersonPhoneMainLabel:@"04741234567" 
     }; 
     MKPlacemark *place = [[MKPlacemark alloc] 
           initWithCoordinate:coords addressDictionary:address]; 

     MKMapItem *mapItem = [[MKMapItem alloc]initWithPlacemark:place]; 
     mapItem.phoneNumber = @"0141343252"; 

     //current location 
     MKMapItem *mapItem2 = [MKMapItem mapItemForCurrentLocation]; 


     NSArray *mapItems = @[mapItem, mapItem2]; 

     NSDictionary *options = @{ 
      MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving, 
      MKLaunchOptionsMapTypeKey: 
      [NSNumber numberWithInteger:MKMapTypeStandard], 
      MKLaunchOptionsShowsTrafficKey:@YES 
     }; 

     [MKMapItem openMapsWithItems:mapItems launchOptions:options]; 


    }else { 
     NSLog(@"tot hier"); 
     // MKMapKit is not available in this OS 
     locationController = [[MyCLController alloc] init]; 
     locationController.delegate = self; 
     [locationController.locationManager startUpdatingLocation]; 
    } 

但由于某种原因,它始终使用谷歌方法。

任何人都可以帮忙!

回答

0

如已经提到的,MapKit已经可用的iOS 6.

之前。

你想要检查的是MKMapItem(不是“MKMapKit”)。

然而,由于documentation for MKMapItem explains(用代码示例):

要确定一个类是否可在给定的iOS 发布运行时,您通常会检查类是否是零。不幸的是, 这个测试对于MKMapItem来说不是干净准确的。 尽管此类 从iOS 6.0开始公开可用,但在此之前它在开发 。尽管该类存在于早期版本中,但您在此版本中不应该尝试使用它。

要确定在运行时是否可以使用地图项目在 应用,测试是否类和 openMapsWithItems:launchOptions:类方法存在。该方法是 直到iOS 6.0才添加到类中。该代码可能类似于 如下:

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

所以此项检查:

if(NSClassFromString(@"MKMapKit")) { 

应该是:

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

或:

Class itemClass = NSClassFromString(@"MKMapItem"); 
if (itemClass && [itemClass respondsToSelector:@selector(openMapsWithItems:launchOptions:)]) { 
+0

谢谢˚F或你的答案!这工作! – Steaphann

0

MkMapKit也可以在ios 4.3中使用,也可能在3.x中! 什么是新的,是(像在所有发行版),MkMapKit的一些新的方法:

你最好检查你需要 (地理编码的具体方法=

查找到你MkMapKit的头导入(如果我正确remeber:MkMapKit.h),有宏限定specifc方法的可用性 ,取决于IOS版本