I am trying to get the user's coordinates when a viewController appears. I need the location in several viewControllers, so I put the locationManager in the appDelegate. My problem is that on the very first viewDidAppear, the coordinates have not yet been found. How should I go about changing this? Thank you all!!!
My AppDelegate has this:
- (NSString *)getUserCoordinates
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return userCoordinates;
}
My viewController gets the coordinates with this:
- (void)viewDidAppear
{
NSString *userCoordinates =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserCoordinates];
}
I've recently implemented the same thing, location manager in AppDelegate so that my ViewControllers all have access to the location data.
Don't forget to implement the CoreLocation delegate methods, particularly didUpdateLocations in order to get the new location data. I would put that in AppDelegate class.
Since, you have a relationship where one object (AppDelegate) needs to notify many viewControllers about a location update, I recommend using NSNotification.
In your AppDelegate, you would write...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// set up location manager
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager startUpdatingLocation];
return YES;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation * newLocation = [locations lastObject];
// post notification that a new location has been found
[[NSNotificationCenter defaultCenter] postNotificationName:@"newLocationNotif"
object:self
userInfo:[NSDictionary dictionaryWithObject:newLocation
forKey:@"newLocationResult"]];
}
And in your ViewController, you wouldn't use the viewDidAppear method. Instead, you would do this...
- (void)viewDidLoad
{
[super viewDidLoad];
// subscribe to location updates
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updatedLocation:)
name:@"newLocationNotif"
object:nil];
}
and you would have a method updatedLocation that looks like this
-(void) updatedLocation:(NSNotification*)notif {
CLLocation* userLocation = (CLLocation*)[[notif userInfo] valueForKey:@"newLocationResult"];
}
You can have other viewControllers also subscribe to notification updates by adding them as observers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With