简体   繁体   English

iOS:启动时为什么“打开定位服务”警报会显示两次?

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

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. 此后立即(在触摸第一个警报按钮之前),它再次显示,这次包含了target属性。

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? 我应该怎么做才能只显示一次目的属性?

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. showUserLocation执行代码,我看到在执行showUserLocation行时显示了第二个警报。 I'll have to do a little more testing to narrow it down exactly, but I think I'm on the right track now. 我必须做更多测试才能精确缩小范围,但我认为我现在处在正确的轨道上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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