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条回答
小情绪 Triste *
2楼-- · 2020-01-27 10:41

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];
查看更多
看我几分像从前
3楼-- · 2020-01-27 10:43

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
    }
}
查看更多
萌系小妹纸
4楼-- · 2020-01-27 10:44

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];
}
查看更多
Ridiculous、
5楼-- · 2020-01-27 10:51

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

查看更多
你好瞎i
6楼-- · 2020-01-27 10:51

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.

查看更多
▲ chillily
7楼-- · 2020-01-27 10:52

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

查看更多
登录 后发表回答