objective-c - 在单例类 (ARC) 中使用 MKReverseGeocoder

标签 objective-c ios xcode singleton reverse-geocoding

我正在尝试为天气功能(全部包含)创建一个单例类,以便我可以在单个分配的对象中更改/更新/调用整个应用程序中的天气数据。

除了一个奇怪的小错误外,我可以正常工作。我在运行 iOS < 5 的设备上使用 MKReverseGeocoder。CLGeocoder 在 iOS 5+ 上运行良好。基本上,这就是应用程序中发生的事情:

在应用委托(delegate)中,在启动时,我创建了我的天气单例的共享实例。除非有一个 UIView 需要报告天气结果,否则这个实例不会做任何事情。加载报告天气的 View 时,如果位置尚未在首选项中静态设置,则该应用会尝试提取您的当前位置。

这行得通,我可以记录设备当前位置的坐标。完成后,我立即开始尝试对这些坐标进行反向地理定位。 MKReverseGeocoder start 方法确实得到执行,我可以记录实例的 isQuerying 属性为真,所以我知道它正在尝试对坐标进行地理定位。 (是的,我将委托(delegate)设置为我的共享实例,该实例的类型为 MKReverseGeocoderDelegate)。

现在这是奇怪的部分。如果我是第一次启动该应用程序,并且第一次将天气 UIView 添加到屏幕,MKReverseGeocoder 会启动但从不调用委托(delegate)方法。如果我然后关闭应用程序并再次打开它(第二次),则会查找当前位置,并且 MKReverseGeocoder 会调用委托(delegate)方法并且一切正常。它只是不想在第一次启动时工作,无论我调用它多少次(我有一个可以启动查找的按钮)。

这真是令人费解。 iOS 5 CLGeocoder 在初始启动和每次后续启动时都能正常工作。 MKReverseGeocoder 在初始启动时不起作用(因为它不调用委托(delegate)方法),但在后续启动时起作用。

相关代码如下:

-(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 时)。对 log 的调用甚至没有被命中(这就是为什么我假设委托(delegate)方法没有被命中)。

任何输入将不胜感激! 谢谢!

最佳答案

我最终弄明白了……好吧,有点。 我没有使用 MKReverseGeocoder,而是对谷歌地图进行静态查找 How to deal with MKReverseGeocoder / PBHTTPStatusCode=503 errors in iOS 4.3?

完美运行。

关于objective-c - 在单例类 (ARC) 中使用 MKReverseGeocoder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10384789/

相关文章:

objective-c - NSProgressIndicator 中的标签/文本?

iOS for Android View.GONE setVisibility 模式

ios - 如果字符串值之一包含 ("),则 Swift Codable 无法解码

xcode - 如何更新Xcode以安装 “UNIX Development Support”?

swift - 标签栏 View Controller [Swift 3.0 - Xcode]

ios - 核心数据反向排序关系获取

iphone - 在 iPhone 应用程序中使用 PayPal API

uinavigationcontroller - 重复调用UINavigationController 的push 导致访问错误

ios - "Unable to simultaneously satisfy constraints"调试时

ios - 从父级到子级的 Swift Pass 事件