Reputation: 2171
I want to use the following code to find a user's location in multiple parts of my app. To avoid complexity, I only want to have one place where I get the user's location. I would assume that I need to put it in my appDelegate and then call it somehow from the other views when I need to use it. Does anyone know what I would need to use to get the location as an NSString on the views/tabs other than my appDelegate? Thank you!
- (NSString *)deviceLocation {
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude];
return theLocation;
}
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
Upvotes: 0
Views: 221
Reputation: 157
You can put your code in app delegate..like this
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
@public
float latitude;
float longituted;
}
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
if (locationManager==nil) {
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
[locationManager startUpdatingLocation];
}
if ([CLLocationManager regionMonitoringAvailable]) {//check service is available or not
[self startregionMonitoring];
}
[self.window makeKeyAndVisible];
return YES;
}
#pragma mark - GET Location
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation");
NSDate *eventData=newLocation.timestamp;
NSTimeInterval howRecent=[eventData timeIntervalSinceNow];
if (abs((howRecent)<15.0)) {
NSLog(@"latitude %+.6f, longitude %+.6f \n",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
CLLocationDistance dist=[newLocation distanceFromLocation:oldLocation]/1000;
NSLog(@"===================distance %5.1f traveled",dist);
latitude=newLocation.coordinate.latitude;
NSLog(@"lat :%+.5f",latitude);
longituted=newLocation.coordinate.longitude;
NSLog(@"long %+.6f",longituted);
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[locationManager stopUpdatingLocation];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
[locationManager startUpdatingLocation];
}
now where you want latitude and longitude you can access [DELEGATE latitude];,[DELEGATE longitude]; where DELEGATE Is #define DELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]
Upvotes: 1
Reputation: 37729
Move all of your location listener and related stuff to the app delegate, and then make NSString *location
property of AppDelegate
, now whenever you get an updated location set that to NSString *location
, in your other ViewControllers
, when you need, access the app delegate's location property.
If you really need an updated location you can use NSNotificationCenter and broadcast a notification about new location update, so the ViewController
which really need an updated location will register itself for Notification about new location and will do stuff accordingly.
Upvotes: 1