Skip to content

Instantly share code, notes, and snippets.

@ryanhanwu
Created December 6, 2012 07:20
Show Gist options
  • Save ryanhanwu/4222448 to your computer and use it in GitHub Desktop.
Save ryanhanwu/4222448 to your computer and use it in GitHub Desktop.
[iOS] Get current location and city name
@interface myViewController: UIViewController <CLLocationManagerDelegate>
CLLocationManager *locationManager;
CLLocation *currentLocation;
@end
@implementation AuthViewController
- (void)viewDidLoad
{
locationManager = [CLLocationManager new];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
currentLocation = [locations objectAtIndex:0];
[locationManager stopUpdatingLocation];
NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
}];
}
@end
@User2004
Copy link

User2004 commented Jan 5, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment