2012-04-30 14 views
1

我正在嘗試爲天氣功能(全部包含)創建一個單例類,以便我可以在整個應用程序中更改/更新/調用天氣數據分配的對象。在單個類中使用MKReverseGeocoder(ARC)

我有它的工作,除了一個小怪異的錯誤。我在運行iOS <的設備上使用MKReverseGeocoder 5. CLGeocoder在iOS 5+上運行良好。基本上,這是在應用程序中發生的事情:

在應用程序委託中,在啓動時,我創建了我的天氣單件的共享實例。除非有UIView預期報告天氣結果,否則此實例不會執行任何操作。當加載報告天氣的視圖時,如果該位置尚未在首選項中靜態設置,該應用會嘗試拉取當前位置。

這個工作,我可以記錄設備當前位置的座標。完成之後,我立即嘗試將這些座標反轉爲地理定位。方法MKReverseGeocoder start會得到執行,我可以記錄該實例的isQuerying屬性是否爲真,所以我知道它正在嘗試對這些座標進行地理定位。 (是的,我將代理設置爲我的共享實例,實例的類型爲MKReverseGeocoderDelegate)。

現在這是奇怪的部分。如果我第一次啓動應用程序,並且第一次向屏幕添加天氣UIView,則MKReverseGeocoder會啓動,但不會調用委託方法。如果我然後關閉應用程序並再次打開它(第二次),則當前位置get被擡起,MKReverseGeocoder調用委託方法並且一切正常。它只是不想在第一次啓動時工作,無論我多次調用它(我有一個可以啓動查找的按鈕)。

這完全是莫名其妙的。 iOS 5 CLGeocoder在首次發佈和隨後的每次發佈時均可正常工作。 MKReverseGeocoder在首次啓動時不起作用(因爲它不調用委託方法),但在後續啓動時會執行。

下面是相關代碼:

-(void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error{ 

    NSLog(@"error getting location:%@", error); 
    self.reverseGeocoder = nil; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"errorGettingLocation" object:nil userInfo:[NSDictionary dictionaryWithObject:error forKey:@"error"]]; 

} 
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)pm 
{ 
    //update placemark and get weather from correct placemark 
    placemark = pm; 
    NSLog(@"singleton placemark: %@",placemark); 
    [self getLocationFromPlacemark]; 
    self.reverseGeocoder = nil; 
} 


- (void) getReverseGeoCode:(CLLocation *)newLocation 
{ 
    NSLog(@"reversGeocode starting for location: %@", newLocation); 
    NSString *ver = [[UIDevice currentDevice] systemVersion]; 
    float ver_float = [ver floatValue]; 
    if (ver_float < 5.0) { 
     //reverseGeocoder = nil; 
     self.reverseGeocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; 
     self.reverseGeocoder.delegate = self; 
     [self.reverseGeocoder start]; 
     if(self.reverseGeocoder.isQuerying){ 
      NSLog(@"self.reverseGeocoder querying"); 
      NSLog(@"self.reverseGeocoder delegate %@", self.reverseGeocoder.delegate); 
      NSLog(@"self %@", self); 
     }else { 
      NSLog(@"geocoder not querying"); 
     } 
    } 
    else { 
     [reverseGeocoder5 reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error){ 


      if([placemarks count]>0){ 
       placemark = [placemarks objectAtIndex:0]; 
       [self getLocationFromPlacemark]; 
      } 
      else{ 
       if (error) { 
        NSLog(@"error reverseGeocode: %@",[error localizedDescription]); 

       } 
      } 
     }]; 

    } 
} 

而且,我設置reverserGeocoder爲(非原子,強)財產和合成它。請記住,第二次乾淨啓動後(當有UIView已經加載的天氣)時,此工作正常。對日誌的調用甚至沒有被擊中(這就是爲什麼我假設委託方法沒有被擊中)。

任何輸入將非常感激! 謝謝!

回答

0

MKReverseGeocoder不存在了...在iOS 5 您必須導入CoreLocation並使用CLGeocoder。下面只是一些例子示例代碼:

    CLGeocoder *geoCoder = [[CLGeocoder alloc] init]; 
     CLLocation *location = [[CLLocation alloc] initWithLatitude:37.33188 longitude:-122.029497]; 

     [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemark, NSError *error) { 
      CLPlacemark *place = [placemark objectAtIndex:0]; 
      if (error) { 
       NSLog(@"fail %@", error); 

      } else { 
       NSLog(@"return %@", place.addressDictionary); 
      } 

     }]; 
+0

正確的,除了在運行iOS <5的設備,如原來的問題說......基本上,你最終不得不如果你想你的應用程序上運行,使用這兩種方法較舊的iOS版本......這是我的問題。 – dcinzona