- Import CoreLocation Framework to your Project
-
#import <CoreLocation/CoreLocation.h> to your .h file
- Add CLLocationManagerDelegate to your .h file
- Now make object of CLLocationManager class
e.g. CLLocationManager *locationManager - After this add following code to ViewDidLoad
locationManager = [[CLLocationManager alloc] init];locationManager.delegate = self;locationManager.desiredAccuracy = kCLLocationAccuracyBest;[locationManager requestWhenInUseAuthorization];[locationManager requestAlwaysAuthorization];locationManager.distanceFilter=kCLDistanceFilterNone;[locationManager startUpdatingLocation]; - Add two Delegate method to your .m file
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{NSLog(@"Latitude:%f Longitude:%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude);
[locationManager stopUpdatingLocation];}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{} - Add Properties to your info.plist file
NSLocationAlwaysUsageDescription = location required
NSLocationWhenInUseUsageDescription = Location is required to find out where you are
In new versions of Xcode and iOS this is mandatory.
If you are testing in simulator then you have to put breakPoint before startUpdatingLocation and at the execution time you have to select city manually through Xcode. You can also add .GPX file for same.