2012-12-05 63 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版本