Gust
Gust

Reputation: 305

How to receive significant location changes on all application instead of only one view?

I'm new to iOS programming and I need to implement a location aware application. I can already use the significant location changes service, however when I leave my view it stops receiving new updates.

If I enter background on that view I still get the updates and everything is fine, but if I change my view to other it stops...

I think it is logical to happen this way but I need to receive updates on my other views as well...

Should I replicate the code for each view I have or can I make it that I receive the update in whatever view I am, like making my application answer instead of each view.

Thanks, GustDD

Upvotes: 0

Views: 181

Answers (2)

Ab'initio
Ab'initio

Reputation: 5418

Add this code in AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:  (NSDictionary *)launchOptions
 {
    if(!locationManager) {
         locationManager = [[CLLocationManager alloc] init];
         locationManager.delegate = self; 
         locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
         locationManager.distanceFilter = kCLDistanceFilterNone;
         [locationManager startUpdatingLocation];
       }
 }

 - (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation  *)newLocation fromLocation:(CLLocation *)oldLocation {
//here you will get upadated location
    //here you can add delegate method call to where you want to use this location or you can create a shared variable

}

Upvotes: 1

Malek_Jundi
Malek_Jundi

Reputation: 6160

You could :

  1. define the location instance in the application delegate so you can access it anytime.

  2. use NSNotificationCenter to post a notification when the location change.

  3. use delegates to pass info when the location changed.

Upvotes: 1

Related Questions