Edward Hung Edward Hung - 2 months ago 24x
Swift Question

how to detect if the location service popup has been displayed previously

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 {


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...


Hoa Hoa

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

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

    // 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.