Search code examples
iosswiftfunctionlocationcllocation

Swift: How to manually call CLLocation


So normally when you try to grab a user's location using CLLocation, you'll use the didUpdateLocations method which(if everything has gone well) will be called when using locationManager.startUpdatingLocations().

But I need to be able to manually call the function that gets the location so I can use a completion handler with it. How would I be able to get the user's location after start updating locations without using the didUpdateLocations method?


Solution

  • You can access the CLLocationManager's location property at any time. This will have the most recently determined location. It may be nil if no location has been determined.

    If you are targeting iOS 9 & later then you can use the requestLocation method to request a single location update. The location update will be delivered to didUpdateLocations as usual. There is no synchronous/blocking "request location, wait for it to update and return the location to me" method.