Request for location services disappears before it can be tapped

Hi

I have an app (still in development) which has been running fine for a while. This morning it suddenly stopped being able to get a location (CLLocationCoordinate2D) from the location manager. I tried clean building and deleting and reinstalling the app on an iPhone and the situation got worse.

Now when I launch the app the dialog that requests access to location services dissappears immediately it is displayed.

I updated the phones IOS a couple of days ago but I'm not sure this is the issue as it was fine this morning.

Does anyone know how I can fix this?

Many thanks

Ps... I found an old solution that recommends moving the location declaration out of the function and into the class but this doesn't work either...

Also, I just tried the same on an iPad and found a little bit of a dodgy workaround (not appropriate for a published app).


Launch the app from XCode.

When it hangs (black screen on the device), press the home button and you will see the permissions dialogue.

Click allow etc.


This works on the iPad, but not the iPhone. On the iPhone the app simply fails to launch, the screen goes black and then its gone again

Both iPad and iPhone are on IOS 11.2.6. Also, the iPhone raises this error in Xcode: "Terminating App due to uncaught exception 'NSInternalInconsistencyException', reason 'Delegate must respond to locationManager:didFailWithError'.


I'm new to IOS development so any pointers are much appreciated.


Cheers

This doesn't solve the issue with the permissions request dissappearing as originally mention, but it got passed the error mentioned in my lasy post (found on Stack Overflow and tweaked as per XCode compile warnings:


extension UIViewController : CLLocationManagerDelegate {

public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {

print("error:: \(error.localizedDescription)")

}

public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {

if status == .authorizedWhenInUse {

manager.requestLocation()

}

}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

if locations.first != nil {

print("location:: (location)")

}

}

}

So I copied this bit of code from Apples WWDC Whats new in Mapkit 2017:


let locationManager: CLLocationManager = {

let manager = CLLocationManager()

manager.requestWhenInUseAuthorization()

manager.startUpdatingLocation()

return manager

}()


It didn't solve MY issue where the request for access dissapears before I can touch it. However...

I installed the entire tandM demo app from Apple and it works just fine.


Looks like the error is elsewhere in my code.

If anyone out there is having issues like mine I highly recommend downloading the sample app from Apple and watch the video!

Accepted Answer

Ok... Looks like this was simply down to me having the first view displayed immediately ask for the users location. By simply deferring the request until the user triggered it, or by setting the initial view to one that didn't ask for the users location, the problem went away... I.e. instead of the app loading to a black screen with the request for access to location services behind it, now it completes loading and the request is in the foreground.

Request for location services disappears before it can be tapped
 
 
Q