每次启动更新位置时不显示警报

Do not show alert each time when startUpdatelocation

我正在开发需要获取位置的定位应用程序。当我进入特定的 ViewController 并且位置已关闭时,它会发出警报,显示开关位于 fine.But 的位置,问题是一旦我说取消 & 下次调用 [locationManager startUpdatingLocation]; 它不会提醒我打开位置。

  - (void)getCurrentLocation {
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        locationManager.distanceFilter = kCLDistanceFilterNone;

        if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {

            [locationManager requestAlwaysAuthorization];
        }

        [locationManager startUpdatingLocation];

    }

Note - I have done changes in plist to NSLocationAlwaysUsageDescription set to The XYZ app would like to use your current location

遗憾的是这是设计使然。这样应用程序开发人员就不能用没完没了的请求来骚扰用户。

您需要做的是检查 [CLLocationManager authorizationStatus],如果 kCLAuthorizationStatusDenied 则显示一条消息,提示用户需要进入他们的设置应用并为您的应用启用位置。

您还应该处理其他授权状态。请参阅 CLLocationManager Documentation - CLAuthorizationStatus

中的列表

检查startUpdatingLocation之前的状态。即

          if([CLLocationManager locationServicesEnabled])
             {
                if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
                    NSLog(@"Add your alert here");
                }
                else
                {
                    [locationManager startUpdatingLocation];
                }
            }
            else
            {
                NSLog(@"Add your alert here");
            }