didUpdateToLocation is not called

*爱你&永不变心* 提交于 2019-12-08 02:43:27

问题


I want to receive location updates. I have added a location delegate to the header:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

and the following functions:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //initialize location manager
    CLLocationManager* locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"SomeViewController"     bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //Some problem
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation{
    //Some action
}

But neither didUpdateToLocation nor didFailWithError are called (same problem on the simulator and on the device). What am I missing?

Thanks in advance,

Luda


回答1:


Are location services for your application enabled?

P.S: it would help to make your location manager an instance variable so it will be retained by ARC after the method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions is completed.




回答2:


I realize this is a necro, but I searched high and low for an answer on this and couldn't find a solution for my similar situation, this seems like the best spot for it.

I made a Delegate object with a custom initializer that sets some flags then runs [self->locationManager startUpdatingLocation], and invoke it as such in a viewDidLoad:

myLocationGetter* __unused getter = [[myLocationGetter alloc]initAndUpdateOnce;

Neither didUpdateToLocation nor didFailWithError was getting called. Seems like ARC was forgetting about my getter object before the callbacks came through. Solved the problem by declaring getter as an instance var in the view controller and initializing it in viewDidLoad.



来源:https://stackoverflow.com/questions/10893884/didupdatetolocation-is-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!