I have multiple view controllers that need to get the user’s location, so I wanted to create a separate class that the ViewControllers can call to get the user’s latest location.
locationManager:didUpdateToLocation:fromLocation returns void. How do I pass the latitude and longitude data back to my ViewControllers as soon as the user’s latitude and longitude is calculated?
I could try writing getters and setters in my locationManaging class, but if I do that, how do I know when to call the latitude getter and longitude getter methods from my ViewController class? How do I hold the ViewController’s main thread to wait for the latitude and longitude values from the locationManaging class?
Thanks!
Create a singleton class which has a
latitudeandlongitudeproperties,startLocatingandendLocating. In the class, create aCLLocationManagerinstance, and set its delegate to be the singleton. InstartLocatingandendLocating, call the appropriate methods of theCLLocationManagerinstance. Make the delegate methods update thelatitudeandlongitudeproperties. In otherViewControllers, read this singleton’slatitudeandlongitudeproperties.To know when to read those properties from another
ViewController, set an observer on these properties (see the NSKeyValueObserving Protocol ReferenceBefore doing this, look up the Internets for existing code.
After doing this, upload it to GitHub with a permissive license.