Add these in info.plist
---------------------------
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
-(void)initLocationManager {
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:100];
if([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
//[self.locationManager requestWhenInUseAuthorization]; // Add This Line
}
if([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){
[self.locationManager requestAlwaysAuthorization]; // Add This Line
}
[self.locationManager startUpdatingLocation];
}
#pragma mark - LocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
NSLog(@"locationManager didUpdateLocations");
[locationManager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:[locations objectAtIndex:0] completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSString* countryName = placemark.country;
NSLog(@"Country Name: %@",countryName);
self.countryNameString = countryName;
NSString *countryCode = placemark.ISOcountryCode;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: countryName, @"countryName",
countryCode, @"countryCode", nil];
if(self.geoLocationDelegate && [self.geoLocationDelegate respondsToSelector:@selector(receivedGeoLocationData:)]) {
[self.geoLocationDelegate receivedGeoLocationData:dict];
}
}];
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSLog(@"Location Manager did fail : %@", [error localizedDescription]);
}
No comments:
Post a Comment