简体   繁体   中英

locationServicesEnabled == YES, but [[CLLocationMagager alloc] init] returns nil

I'm a bit puzzled. Consider the following code:

    if([CLLocationManager locationServicesEnabled])
    {
            if(self.locationManager == nil)
            {
                    self.locationManager = [[CLLocationManager alloc] init];
            }
            // do something with the location manager...
    }

self.locationManager is set to 0x0. The conditions for both alternatives are met. I checked this using a breakpoint at self.locationManager = ... and the line is called on each start of the app. Most interestingly, a MKMapView in the same view controller is able to show the user's location.

Thanks for any insights, Chris

The problem was the declaration of locationManager as

    @property(weak, nonatomic) CLLocationManager* locationManager;

The framework does not retain each location manager instance, so I should have declared

    @property(strong, nonatomic) CLLocationManager* locationManager;

instead, which works perfectly.

Thanks to LocoMike for pointing me into the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM