I am trying to simply get the user's current device location and store it in a global variable called userLoc. I have this code to call didUpdateLocation:
self.locationManager.requestLocation()
print("aaa ", self.userLoc)
and this to set the userLoc to the current location:
func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
self.userLoc = location
}
}
userLoc is defined like this:
var userLoc: CLLocation?
but when I run the app and try to print userLoc, it ALWAYS prints:
aaa nil
The Privacy - Location When In Use Usage Description is set in the Info.plist. I honestly dont know what else to try. I've been banging my head against the wall for a while trying to fix this. Any help you could give would be very much appreciate. Thank you.
printline is being called long before the location is updated. - rmaddysleepfor a couple seconds to see if it gives it time to update. Nothing - Elliot Aldersonsleep, especially from the main thread. Simply move yourprintto just after the line where you setself.userLoc. And also check the console for any messages related to missing privacy settings. And make sure you have a call toself.locationManager.requestWhenInUseAuthorization. There are many samples and tutorials on how to properly work with CoreLocation. - rmaddyself.locationManager.requestWhenInUseAuthorizationi get an error:Expression resolves to an unused function- Elliot Alderson