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]