I'm using CoreLocation and am starting the locationManager from within my applications AppDelegate. Example Code below...
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
// start location manager
if([CLLocationManager locationServicesEnabled])
{
myLocationManager_ = [[CLLocationManager alloc] init];
myLocationManager_.delegate = self;
[myLocationManager_ startUpdatingLocation];
}
else
{
// ... rest of code snipped to keep this short
And in this method we see the updated location.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
NSLog(@"AppDelegate says: latitude: %@", currentLatitude);
// ... rest of code snipped
Now, within other areas of my application I need to determine the users current location (latitude, longitude). I could incorporate the code above into the ViewControllers that need current location but then I would have multiple instances of CLLocationManager running (I think) - and why duplicate this code?? Isn't there a way that, from other ViewControllers, I can just grab the location information from the AppDelegate?
PS - I'm using Xcode 4.3 w/ ARC
To do this, declare the variable as a property in your appDelegate:
@property (nonatomic, retain) NSArray *array;
(@synthesize in your .m too)
Then in your view controllers, create an appDelegate variable:
AppDelegate *appDelegate=(AppDelegate*)[[UIApplication sharedApplication] delegate];
then you can do:
NSLog(@"%@", appDelegate.array);