1

I want to check the user device location is in India or not and, if location is not India then user can't able to use the app, I did it using region setting but I want to do it using GeoLocation???

MahajanSagar
  • 740
  • 1
  • 6
  • 21

1 Answers1

2

Objective-C

CLLocation *location = [[CLLocation alloc]
                            initWithLatitude:+38.4112810
                            longitude:-122.8409780f];
CLGeocoder*myGeocoder = [[CLGeocoder alloc] init];

[myGeocoder
     reverseGeocodeLocation:location
     completionHandler:^(NSArray *placemarks, NSError *error) {

 if (error == nil && placemarks.count > 0){ 

      CLPlacemark *placemark = placemarks[0];

     NSLog(@"Country = %@", placemark.country); 

     NSLog(@"Postal Code = %@", placemark.postalCode);

     NSLog(@"Locality = %@", placemark.locality);
 }
 else if (error == nil && placemarks.count == 0){

     NSLog(@"No results were returned.");

  }
   else if (error != nil) {

      NSLog(@"An error occurred = %@", error);

    }
}];

Swift

func gecode(location:CLLocation)
{

    let geoCoder = CLGeocoder()

    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        if(error != nil)
        {
            return
        }

        if let placeMark = placemarks?[0] {

            if let country = placeMark.addressDictionary!["Country"] as? String {

                if country == "India" {

                    //
                }

            }

        }

    })

}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87