发布 MK 反向地理编码器使我的应用程序崩溃!
Posted
技术标签:
【中文标题】发布 MK 反向地理编码器使我的应用程序崩溃!【英文标题】:Releasing MKReverseGeocoder makes my app crash! 【发布时间】:2011-04-18 15:32:44 【问题描述】:我需要知道我的用户来自哪里,所以我制作了一个小单例对象,它获取坐标并使用 mapkit 获取我需要的国家代码。
这是我的头文件:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#define TW_GEO_CODER_CHANGED_STATE @"TW_GEO_CODER_CHANGED_STATE"
@interface TWGeoCoder : NSObject <CLLocationManagerDelegate, MKReverseGeocoderDelegate>
CLLocationManager *locationManager;
MKReverseGeocoder *geoCoder;
NSString *currentCountryIsoCode;
+ (TWGeoCoder*) sharedTWGeoCoder;
-(void) startGeoCoder;
-(void) stopGeoCoder;
@property (nonatomic, retain) NSString *currentCountryIsoCode;
@end
以及实现:
#import "TWGeoCoder.h"
@implementation TWGeoCoder
static TWGeoCoder* _singleton;
+ (TWGeoCoder*) sharedTWGeoCoder
@synchronized([TWGeoCoder class])
if (_singleton == nil)
_singleton = [[TWGeoCoder alloc] init];
return _singleton;
- (void)startGeoCoder
if (locationManager == nil)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.purpose = NSLocalizedString(@"#LocalizationPurpose",nil);
[locationManager startUpdatingLocation];
- (void) stopGeoCoder
if (geoCoder != nil)
[geoCoder cancel];
[geoCoder release];
geoCoder = nil;
if (locationManager != nil)
[locationManager stopUpdatingLocation];
[locationManager release];
locationManager = nil;
#pragma mark -
#pragma mark locationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
if (geoCoder == nil)
geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
geoCoder.delegate = self;
[geoCoder start];
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
[self stopGeoCoder];
#pragma mark -
#pragma mark reverseGeocoder Delegate
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
self.currentCountryIsoCode = placemark.countryCode;
[[NSNotificationCenter defaultCenter] postNotificationName:TW_GEO_CODER_CHANGED_STATE
object:self];
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
[self stopGeoCoder];
#pragma mark -
#pragma mark Synthesizes
@synthesize currentCountryIsoCode;
@end
好吧,调用 stopGeoCoder 方法会使我的应用崩溃,甚至通过 performSelectorOnMainThread 调用它...
问题出在以下几行:
if (geoCoder != nil)
[geoCoder cancel];
[geoCoder release];
geoCoder = nil;
MKReverseGeocoder 似乎在我尝试释放它时变得非常生气! 我只在“didFail”方法上发生崩溃。 实际上,当它找到地标时,另一个类会收到通知,执行某些操作并调用 stopGeocoder 并且......它不会崩溃! 什么鬼?
【问题讨论】:
【参考方案1】:你应该参考另一篇关于堆栈溢出的帖子的解决方案:
cllocation and mkreversegeocoder
【讨论】:
以上是关于发布 MK 反向地理编码器使我的应用程序崩溃!的主要内容,如果未能解决你的问题,请参考以下文章