0

When I run locationManager.requestWhenInUseAuthorization() for the first time, the standard "Allow AppName to access your location while you use the app" pops up and never again based on the standard IOS design (regardless of whether the user chose Allow of Dont Allow). The following code is in ViewDidLoad inside my main view controller

// Check location status
    if locationAuthStatus == CLAuthorizationStatus.AuthorizedWhenInUse {
        self.displayMessage.hidden = false
        self.displayMessage.text = "Waiting for GPS Signal..."

    } else {

        locationManager.requestWhenInUseAuthorization()
    }

The problem I have with this is if the user quits the program, disable the location service, and come back in, there would be no popup asking the user to enable location as it was already shown. So I want to add another custom pop up to ask for permission if the following two condition is true

  1. initial popup has been displayed previously
  2. location service is not enabled.

Initially I had my popup code just after locationManager.requestWhenInUseAuthorization(). However, that was causing a problem. If the user used the app for the first time, he will be prompted with the default popup, then there would be my popup right after that...

Thanks,

user172902
  • 3,541
  • 9
  • 32
  • 75
  • I've seen this before on other apps, I think people generally direct people to the Settings app and ask users to update the settings there.. You can also launch the Settings app from yours [example](http://stackoverflow.com/questions/20840089/open-settings-app-from-another-app-programmatically-in-iphone) – Wez Aug 04 '16 at 15:15
  • That was exactly what I did and I would received two popups upon first launch. One that is the default popup when I ask for requestWhenInUseAuthorization, the other one is mine. So basically I need a way to detect if the first one has shown already – user172902 Aug 04 '16 at 15:19

2 Answers2

5

You can check the authorizationstatus to know if the pop-up has been popped or not.

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
//Status possible:
// User has not yet made a choice with regards to this application
    kCLAuthorizationStatusNotDetermined = 0,

    // This application is not authorized to use location services.  Due
    // to active restrictions on location services, the user cannot change
    // this status, and may not have personally denied authorization
    kCLAuthorizationStatusRestricted,

    // User has explicitly denied authorization for this application, or
    // location services are disabled in Settings.
    kCLAuthorizationStatusDenied,

    // User has granted authorization to use their location at any time,
    // including monitoring for regions, visits, or significant location changes.
    kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),

    // User has granted authorization to use their location only when your app
    // is visible to them (it will be made visible to them if you continue to
    // receive location updates while in the background).  Authorization to use
    // launch APIs has not been granted.
    kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),

    // This value is deprecated, but was equivalent to the new -Always value.
    kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways

If you get a kCLAuthorizationStatusNotDetermined, it means that the pop-up is not shown (the user has not made a choice). Then, depend on the authorizationstatus, you can direct the user to setting screen or continue to update the location.

PS: You should implement the delegate below and see how it works when you call startUpdatingLocationon your CLLocationManager (when the pop-up is not popped and when the pop-up is popped).

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
0

In Swift, this property is now an enum. You can check it with:

if CLLocationManager.authorizationStatus() == .notDetermined {
    //popup was not shown
}
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223