Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

best way to use CoreLocation across multiple views

I have two views in my app, one is a general view where CoreLocation works away calculating the users location while the user is doing other stuff in the view. The second view is accessed by the user when they touch a button allowing them to locate themselves more accurately using a mapview and MapKit, i would like the mapview in this view to show the location that CoreLocation has already identified in the first view AND to continue displaying this location based on updates from CoreLocation in the other view.

Is the best way here to create a singleton that encapsulates the CoreLocation stuff and have this referenced in the view with the map, or to use notifications ? or to use some other better practice for my scenario ?

Thanks

like image 893
Matt Avatar asked Nov 27 '25 20:11

Matt


2 Answers

I have a couple of apps that use CoreLocation in multiple places. From what I've read, you definitely want there to be just one instance of CLLocationManager. It's worked great as a singleton for me.

Hope this helps!

like image 82
donkim Avatar answered Nov 30 '25 09:11

donkim


If I were you, I would do it this way:

  1. Decide which view is going to be always loaded. I assume, you want CalculatingView is loaded all the time, and MapView will be loaded/unloaded based on the user action.

  2. Allocate and initialize a pointer to CLLocationManager inside CalculatingView. This will provide location property and also call delegate messages. Since the CalculatingView is loaded and retained, this pointer is always working too.

  3. Set CLLocationManager's delegate to be CalculatingView, which might also be called self, if this view has allocated and initialized CLLocationManager pointer.

  4. Implement delegate methods of CLLocationManager, in CalculatingView

  5. If you like to, you can have MapView to be allocated and initialized within CalculatingView. But it's ok to have it in other places, as long as you can send message to MapView. Make sure they are valid by checking if it's not nil or if it respondsToSelector.

  6. When the CLLocationManager's delegate, which is CalculatingView receives messages, send a message to MapView.

  7. It's like relaying messages, but the messages that MapView should respond to don't have to be the same messages sent to CalculatingView like delegate method calls from CLLocationManager

  8. By checking if MapView is valid, meaning if it's loaded to be displayed, you can decide to send messages to MapView or not

The essence is to decide which view is loaded consitently, to use delegate methods for sending(or relaying) messages to other pointers(in this cases, MapView pointer).

The singleton is good, but unless you are going to use CLLocationManager from multiple places, like more than 3~4 places, it's not that necessary, I think

Hope I didn't confuse you. Based on what you posted, it seems like this way can be simple solution for your goal. If I didn't catch your true intention, please let me know.

like image 23
petershine Avatar answered Nov 30 '25 08:11

petershine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!