'Current location permission dialog disappears too quickly
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.
Solution 1:[1]
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 2:[2]
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 3:[3]
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 4:[4]
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 5:[5]
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 6:[6]
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 7:[7]
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 8:[8]
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 9:[9]
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 10:[10]
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 11:[11]
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;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
