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

二次信任 提交于 2019-11-30 14:24:56

问题


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 a button of first alert is touched), it shows again, this time with the purpose property included.

When the second alert is dismissed, the first alert is still there.

This is a little annoying, and I would expect it to be confusing to the users.

What can I do to only show it once, with the purpose property?


回答1:


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.



来源:https://stackoverflow.com/questions/8967173/ios-why-does-turn-on-location-services-alert-show-twice-upon-startup

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