为什么在iPhone SDK 4.0中没有调用CLLocationManager委托?

问题描述 投票:12回答:5

这是我在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模拟器和设备。

是什么原因 ?

iphone core-location cllocationmanager cllocation
5个回答
21
投票

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

.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];

我希望它有所帮助。


2
投票

您的代表中的-locationManager:didFailWithError:是否会被调用?我只是想,也许你在某个时候拒绝访问位置数据,现在不会得到提示,但访问被拒绝。


1
投票

我疯狂地想弄清楚为什么使用CLLocationManager的几个类中的一个永远不能调用任何CLLocationManagerDelegate方法。

事实证明:该类是从非主线程初始化的,因此它的CLLocationManager是在非主线程上创建的......它甚至可能是一个临时线程。无论如何,用下面的方法包装调用来初始化我的类就是调用CLLocationManager委托方法所需的全部内容。

dispatch_sync(dispatch_get_main_queue(), 
                    ^{
                         // create your class that will create a CLLocationManager here.
                     });

所以底线:不要在除主线程之外的任何东西上创建CLLocationManager,否则你永远不会调用你的委托方法!我曾经知道这一点......好久不见了......叹了口气。浪费了一天再次想出这个!由于我读过的所有CLLocationManager SO都没有提到过,我在这里发布它。希望它可以帮到某人!


0
投票

你确定你没有在位置取消分配位置管理器吗?您所拥有的代码显示它是在didFinishLaunching ...方法中分配但后来被遗忘(虽然它仍然保留,因此仍然可以工作)。

你甚至看到初始弹出窗口询问你是否可以获得用户位置?


0
投票

将代码放在viewController“推送到navigationController”中。

我的代码是。

在SomeViewController.h中

#import <MapKit/MapKit.h>
@interface SomeViewController : UIViewController<CLLocationManagerDelegate>{
}
@property (nonatomic, strong) CLLocationManager *locationManager;
@end

在SomeViewController.m中

#import "SomeViewController.h"

@implementation SomeViewController


-(void)findLocation{
    if ([CLLocationManager locationServicesEnabled]) {
        if (!self.locationManager) {
            self.locationManager = [[CLLocationManager alloc] init];
            self.locationManager.delegate = self;
            self.locationManager.distanceFilter = kCLDistanceFilterNone;
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            [self.locationManager startUpdatingLocation];
        }
    }
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    NSLog(@"didUpdateLocations");

    CLLocation *lo = [locations objectAtIndex:0];
    NSLog(@"latitude = %f, longitude = %f", lo.coordinate.latitude, lo.coordinate.longitude);

    [manager stopUpdatingHeading];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
    NSLog(@"didUpdateToLocation");}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"didFailWithError");
    [manager stopUpdatingLocation];
}

我认为的问题是因为ARC,发布了locationManager。

© www.soinside.com 2019 - 2024. All rights reserved.