这是我的 AppDelegate 类中的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = 1000; // 1 Km
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
}
这是我的 AppDelegate 类中的委托方法
//This is the delegate method for CoreLocation
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
//printf("AppDelegate latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
它在 3.0、3.1、3.1.3 中工作,但在 4.0 模拟器和设备中都不起作用。
是什么原因 ?
我有一个类似的错误,现在已经解决了。问题是在本地声明 locationManager 变量。将其声明为类变量,并确保它直接保留或通过属性保留(如果使用 ARC,则为强保留)。这样就解决了问题!问题是 de locationManager 变量被释放并且从未更新委托。这里是代码:
.h file:
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate> {
@private
MKMapView *_mapView;
CLLocationManager *_locationManager;
}
@property(nonatomic, strong) CLLocationManager *locationManager;
@end
.m file:
self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
我希望它有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)