代码之家  ›  专栏  ›  技术社区  ›  Biranchi

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

  •  12
  • Biranchi  · 技术社区  · 14 年前

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

    原因是什么?

    5 回复  |  直到 6 年前
        1
  •  21
  •   Igor Filippov    12 年前

    我也有类似的错误,现在解决了。问题是在本地声明locationmanager变量。将其声明为类变量,并确保直接或通过属性retain(如果使用arc,则为强)将其保留。这就解决了问题!问题was 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
  •  2
  •   Thomas Müller    14 年前

    -locationManager:didFailWithError: 在你的代表中有没有接到过电话?我只是在想,也许你在某个时候拒绝了对位置数据的访问,现在不会得到提示,但是访问被拒绝了。

        3
  •  0
  •   Kendall Helmstetter Gelner    14 年前

    您确定没有在某个地方解除分配位置管理器吗?你的代码显示它正在didfishingsunching中分配…方法,但被遗忘(尽管它仍然保留,所以应该仍然有效)。

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

        4
  •  0
  •   Howard    11 年前

    将代码放入“推送到导航控制器”的视图控制器中。

    我的代码是。

    在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被释放了。

        5
  •  0
  •   Smartcat    6 年前

    我想知道为什么只有一个使用cllocationManager的类永远无法接到 任何 cllocationManagerDelegate方法的。

    结果是:该类是从非主线程初始化的,所以它的cllocationManager是在非主线程上创建的…它甚至可能是一个临时线程。无论如何,用下面的代码包装初始化我的类的调用是使我的cllocationManager委托方法被调用所需要的。

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

    所以底线是:除了主线程之外,不要在任何地方创建cllocationmanager,否则您将永远无法调用委托方法!我曾经知道…只是很久了…叹息。浪费了一天的时间来重新考虑这个问题!因为我读到的所有cllocationmanager-so-i s都没有提到这个,所以我把它贴在这里。希望它能帮助别人!