0

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

Gust
  • 195
  • 1
  • 15

2 Answers2

1

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.

Malek_Jundi
  • 6,140
  • 2
  • 27
  • 36
1

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

}
Ab'initio
  • 5,368
  • 4
  • 28
  • 40