Question

Heres the scenario. I have two UIViewControllers. Lets say, PresentinLondonViewController and NotPresentinLondonViewController. The view that i want to load first should depend on the current user. Lets say if and only if the current users location is London, then i want PresentViewController to show up otherwise NotPresentViewController.

I ve done a lot of research, couldn't find anything concrete even tried my hand on protocol. Doesn't works for me.

Was it helpful?

Solution

You can use CLLocationManager to get your current position , and after that you can do reverse geocoding to get the address from the coordinates.

In that address, if it's accurate you'll find the word 'London' to match your needs.

Do this in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions :

CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.delegate = self;
//Here you set the Distance Filter that you need
manager.distanceFilter = kCLDistanceFilterNone;
// Here you set the Accuracy 
  manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[manager startUpdatingLocation];

And in the delegate function:

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{


        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
        [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            //Picked the first placemark
             CLPlacemark *placemark = placemarks[0];
           //you can play and see how to grab the key name that you want.
             NSLog(@"Address %@",[NSString stringWithFormat:@"%@ %@",placemark.thoroughfare,addressNumber]);
          //Do your additional setup here

        }];
        [geocoder release];
        [location release];

}

And if you want to delay the loading so you can get the time to grab all of the info then you can present another viewcontroller which has the splash screen on it and do the checking there/or in the appDelegate.

OTHER TIPS

get current latLong from CLLocationManager for that you have to use

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
//for iOS 6+
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation 
{
//for lower then iOS 6
}

for getting address from your lat and long use this url

    NSString *urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=%f,%f",YourLatitude,YourLongitude];

You can use MKReverseGeocoder to get MKPlacemark , you can get apple sample code of CurrentAddress

or here is another link

objective c how to get placemarks of given coordinates

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top