iOS: Why does “Turn on Location Services” alert show twice upon startup?

后端 未结 1 631
清歌不尽
清歌不尽 2021-01-01 07:30

When I have location services disabled, this alert shows up twice. The first time is without the location manager purpose property displayed. Immediately after that (before

相关标签:
1条回答
  • 2021-01-01 07:50

    I had both a map controller object and a location manager object instantiated in my app delegate.

    mapController = [[[MapController alloc] initWithFrame:CGRectMake(0, 0, 0, 0)] retain];
    [self restartLocationManager];
    

    However, the location manager purpose property is not set until the location manager is instantiated in this code:

    - (void) restartLocationManager {
        if (locationManager)
            [locationManager release];
    
        locationManager = [[[CLLocationManager alloc] init] retain];
        locationManager.purpose = NSLocalizedString(@"Location Service Purpose", nil);
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
        [locationManager startUpdatingLocation];
    }
    

    So this was a clue that something in the initialization of the map was triggering the first alert.

    Because I declined to turn location services on in the first alert, the map controller initialized and saw a need to show the alert. The map controller initialization is this (it is part of a singleton, and needs some cleanup in that regard, but ignoring that...):

    - (id) initWithFrame:(CGRect)aFrame {
        @synchronized(self) {
            if (!theMap) {
                if (!self) self = [super init];
    
                theMap = [[[MKMapView alloc] initWithFrame:aFrame] retain];
                theMap.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
                theMap.showsUserLocation = YES;
                theMap.delegate = self;
        }
        return self; 
    
    }
    

    Stepping through the code, I saw the second alert show up when the showUserLocation line was executed. I'll have to do a little more testing to narrow it down exactly, but I think I'm on the right track now.

    0 讨论(0)
提交回复
热议问题