views:

380

answers:

1

Hi

I am using a custom class to update user location. When initializing the class, I create a CLLocationManager object to be used by the class

CLLocationManager *locManager = [[CLLocationManager alloc] init];
[self setLocationManager:locManager];

The line returning a nil is CLLocationManager *locManager = [[CLLocationManager alloc] init]; which is what causes an issue.

This code works great most of the times. But on one particular device (OS Version 3.1), the init returns a nil. Any idea why CLLocationManager would return a nil on initialization?

Thanks.

+1  A: 

When an application first initializes location services the OS asks the user if this is OK. If they say no, it's possible on subsequent runs to get nil. You may want to check for this and gracefully degrade features.

Ramin
But while testing on this device I always selected the "OK" button. At one point in time it crashed (again due to location services) and from then on, every time the value for CLLocationManager is nil. Shouldn't the user get the prompt?
lostInTransit
I was pointing out one situation where location services could be returning nil (although technically you should be getting a locationManager:didFailWithError). If you're not then something else is going on. Might want to try resetting location warnings in Settings and see if that lets you start with a clean slate.
Ramin
I guess didFailWithError being the CLLocationManagerDelegate's method, it won't be fired unless CLLocationManager has a value and its delegate is set? Here the CLLocationManager object itself is nil. Adding the code and highlighting the erroneous line
lostInTransit
D'oh! Should have paid more attention to your original question. I saw that behavior one time when user said NO. Adding guard code around it and degrading features dealt with it. Haven't had issues with it since. I'd suggest a process of elimination to see if it's user prefs, memory-management, the app, the OS, or the device. Hard to tell without more contextual information.
Ramin
Thanks. I've put in a check. But still sounds fishy, if location services is off, the error method should be fired. Why is CLLocationManager nil?
lostInTransit
It does sound fishy. When I got it I assumed it was because of all the GPS initialization and caching that needs to happen, but didn't really dig that deeply into it. If you can repeat it reliably on that device, you may want to run the app through Instruments and watch for leaks or low-memory conditions.
Ramin