To find out the country of the user please use the code below:
1- Add the coreLocation Framework to your project and import #import <corelocation corelocation.h="">
2- Set the CLLocationManagerDelegate as UIViewController
@interface LocationViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
3- And in LocationViewController.m file use the code below.
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
if([[[UIDevice currentDevice]systemVersion] floatValue]>=8.0?YES:NO)
{
[locationManager requestAlwaysAuthorization];
}
geocoder = [[CLGeocoder alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
NSLog(@"user contry==%@",placemark.country);
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
[locationManager stopUpdatingLocation];
}
0 Comment(s)