Current location permission dialog disappears too

2020-01-27 10:13发布

My app takes the user's 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 user's location when the app loads up to try and force the alert to show, but that didn't work.

11条回答
甜甜的少女心
2楼-- · 2020-01-27 10:54

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;
}

查看更多
仙女界的扛把子
3楼-- · 2020-01-27 10:59

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

查看更多
萌系小妹纸
4楼-- · 2020-01-27 11:04

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.

查看更多
太酷不给撩
5楼-- · 2020-01-27 11:05

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.

查看更多
唯我独甜
6楼-- · 2020-01-27 11:06

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 :)

查看更多
登录 后发表回答