Current location permission dialog disappears too quickly

Objective CIosCore Location

Objective C Problem Overview


My app takes the users location, gets the co-ordinates , and provides a distance to or from their destination or origin. All these possible destinations are shown in a table view, so I'm getting the users co-ordinates at the same time as populating the table. The only thing is, the alert view that asks for the users location appears then disappears so quickly it's impossible to click it!

Is there any way to manually present this alert when the app first loads? I tried getting the users location when the app loads up to try and force the alert to show, but that didn't work.

Objective C Solutions


Solution 1 - Objective C

While difficult to track down, the solution for this is quite simple.

Through much trial and error I found out that while the location access dialog pops up when you try to access any location services in the app for the first time, the dialog disappears on its own (without any user interaction) if the CLLocationManager object is released before the user responds to the dialog.

I was creating a CLLocationManager instance in my viewDidLoad method. Since this was a local instance to the method, the instance was released by ARC after the method completed executing. As soon as the instance was released, the dialog disappeared. The solution was rather simple. Change the CLLocationManager instance from being a method-level variable to be a class-level instance variable. Now the CLLocationManager instance is only released once the class is unloaded.

Solution 2 - Objective C

Same symptom, different cause: do not to call startUpdatingLocation more than once in a row.

I had accidentally structured things such that the code was unintentionally calling startUpdatingLocation twice in a row, which is apparently bad. It might also have had something to do with choice of queue since I was waiting to start updating pending the result of a network request, but I didn't need to do any GCD magic to fix it...just needed to make sure I didn't repeat the start.

Hope someone's able to benefit from my pain. :)

Solution 3 - Objective C

I have faced the similar situation. After debugging I found

let locationManager = CLLocationManager()

is called in a method scope, but it should be called globally.

Why?

In a nutshell, locationManager has been released after the method had returned. But it shouldn't be released until user give or deny permission

Solution 4 - Objective C

I fall into the same issue (at least by symptoms). In my case the problem was in the - (void)applicationWillResignActive:(UIApplication *)application; method, where I was releasing my CLLocationManager instance as part of preparing for background transition. When I removed it and left it only in - (void)applicationDidEnterBackground:(UIApplication *)application; the problem is gone.
The tricky part is that Core Location alert DO suspend your application while it still in foreground.
Hope that it will help you, took me a lot of time to found that bastard :)

Solution 5 - Objective C

I know this is a very late reply. But it may help someone. I also faced the same problem and spent an hour to identify the issue. At first my code was like this.

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];

CLLocation *location = locationManager.location;
//my stuff with the location

    [locationManager release];

Now the location alert disppeared quickly. When I uncomment the last line it is working correctly.

   // [locationManager release];

Solution 6 - Objective C

I ran into this problem, also, but the solution in my case turned out to be completely different than the accepted answer.

In my app, I was calling stopUpdatingLocation from applicationWillResignActive. This was a problem because applicationWillResignActive is called when the permission dialog appears. This was causing stopUpdatingLocation immediately after startUpdatingLocation, which is why the dialog would immediately disappear.

The solution was simply to call stopUpdatingLocation from applicationDidEnterBackground instead.

Solution 7 - Objective C

This was happening to me while using the iOS Simulator. I determined that it was occurring because my Run Scheme was simulating a location. I think this has the same effect as calling locationManager.startUpdatingLocation() at launch and so it was closing the dialog.

Un-checking the "Allow Location Simulation" checkbox in the Edit Schemes dialog fixed the issue. Once it works as you want it to and the permission is set, you can re-enable the location simulation and the simulator will work fine from then on.

Solution 8 - Objective C

Swift 4 and iOS 11:

Be sure to have added privacy lines (both always and whenInUse) to your .plist file and add CoreLocation Framework to your project

The location permission dialog appears correctly when I've changed :

locationManager.requestAlwaysAuthorization()

with:

locationManager.requestWhenInUseAuthorization()

P.S.: I've tried ALL advices and all fails (request authorization to viewDidLoad, var instead of let for locationManager, don't start startUpdatingLocation() after request..I think it's a bug and I hope they will resolve it as soon as possible..

Solution 9 - Objective C

I had the locationManager as instance var but still that did not help in Swift 5, Xcode 11 (see below):

class MapViewController: UIViewController {
    
    var locationManager: CLLocationManager {
        let locationManager = CLLocationManager()
        locationManager.desiredAccuracy = .greatestFiniteMagnitude
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        return locationManager
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.startUpdatingLocation()
    }
}

However, making locationManager var lazy fixed the issue:

class MapViewController: UIViewController {

    lazy var locationManager: CLLocationManager = {
        let locationManager = CLLocationManager()
        locationManager.desiredAccuracy = .greatestFiniteMagnitude
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        return locationManager
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.startUpdatingLocation()
    }
}

Solution 10 - Objective C

SWIFT 4 @Zoli solution will look like:

class WhateverViewController: UIViewController {
    let locationManager = CLLocationManager() // here is the point of the @Zoli answer
    
    // some code
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // some other code
        locationManager.requestWhenInUseAuthorization()
        // some other code
    }
}

Solution 11 - Objective C

you most define locationManager variable as global object.

@interface ViewController : UIViewController
{
    CLLocationManager *locationManager;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    [locationManager startUpdatingLocation];
}

Solution 12 - Objective C

I met same situation of yours.

  • My solution was changed from local variable to member instance.
  • The cause was that the local?instance was invalid after the method was finished which includes the local the variable(of extend my locationManager)
  • My Env.: Xcode9.3.1

#import @interface ViewController ()

@end

@implementation ViewController @synthesize locManager; // after

  • (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.

    //MyLocationService *locManager = [[BSNLocationService alloc]init:nil]; // before. the loc. delegate did not work because the instance became invalid after this method. self->locManager= [[MyLocationService alloc]init:nil]; // after locManager.startService; }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionglenn sayersView Question on Stackoverflow
Solution 1 - Objective CZoliView Answer on Stackoverflow
Solution 2 - Objective CclozachView Answer on Stackoverflow
Solution 3 - Objective CAnkur LahiryView Answer on Stackoverflow
Solution 4 - Objective CArielView Answer on Stackoverflow
Solution 5 - Objective CRamaraj TView Answer on Stackoverflow
Solution 6 - Objective CAlan KinnamanView Answer on Stackoverflow
Solution 7 - Objective CPaoloView Answer on Stackoverflow
Solution 8 - Objective CAlessandro OrnanoView Answer on Stackoverflow
Solution 9 - Objective CkoiraView Answer on Stackoverflow
Solution 10 - Objective Cwm.p1usView Answer on Stackoverflow
Solution 11 - Objective CMahdi NiliView Answer on Stackoverflow
Solution 12 - Objective Cuser2058374View Answer on Stackoverflow