为什么在iPhone SDK 4.0中没有调用CLLocationManager委托? [英] Why the CLLocationManager delegate is not getting called in iPhone SDK 4.0?

查看:78
本文介绍了为什么在iPhone SDK 4.0中没有调用CLLocationManager委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在AppDelegate类中的代码

This is the code that I have in my AppDelegate Class

- (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类中的委托方法

And this is the delegate method i have in my AppDelegate Class

    //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模拟器和设备上工作。

Its working in 3.0, 3.1, 3.1.3 , but its not working in 4.0 simulator and device both.

是什么原因?

推荐答案

我有类似的错误,现在解决了。问题是在本地声明locationManager变量。将其声明为类变量,并确保直接或通过属性保留(如果使用ARC则保持强大)。这解决了问题!问题是de locationManager变量已发布,并且从未更新过委托。这里代码:

I had a similar error, and is solved now. The issue is declaring locationManager variable locally. Declare it instead as a class variable and make sure it is retained either directly or via property retain (or strong if you use ARC). That solves the problem! The issue was de locationManager variable was released and never updated the delegate. Here the code:

.h文件:

#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文件:

self.locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];

我希望它有所帮助。

这篇关于为什么在iPhone SDK 4.0中没有调用CLLocationManager委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆