Zawarudio
Zawarudio

Reputation: 85

Get user Coordinates in iOS simulator at the app launch


I was asked to code a small app for iOS in Objective C that's supposed to choose the closest object of a user, in a set of coordinates
The plan was to load the coordinates of the user only once, when the application loads for the first time.
Here's my current code :

(void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.headingFilter = 1;
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    [self.locationManager startUpdatingLocation];

    self.userLat = self.locationManager.location.coordinate.latitude;
    self.userLon = self.locationManager.location.coordinate.longitude;
    NSLog(@" ----------------------------- ");
    NSLog(@"User lat : %lf", self.userLat);
    NSLog(@"User lon : %lf", self.userLon);
    NSLog(@" ----------------------------- ");
}

I don't have a Apple iOS developper program, so I'm stuck with the iOS simulator from xCode. I try to set some custom coordinates in the iOS simulator through debug, like this :

When i execute the app (obviously after setting coordinates), the log has userLat and userLon to 0.00..

Apr  6 03:16:20 Locals-Mac.local app[5683]:  ----------------------------- 
Apr  6 03:16:20 Locals-Mac.local app[5683]: User lat : 0.000000
Apr  6 03:16:20 Locals-Mac.local app[5683]: User lon : 0.000000
Apr  6 03:16:20 Locals-Mac.local app[5683]:  ----------------------------- 

I remember hearing somewhere that the iOS simulator don't work with the CoreLocation library (It might be right or wrong, i wouldn't know).
Is it the reason the lat/lon values are 0?
If the reason is indeed the iOS simulator, does this code give user current location?

Thanks in advance!

Edit :

With the answer and comments, i'm now using the didUpdateLocations callback function :

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    self.userLat = location.coordinate.latitude;
    self.userLon = location.coordinate.longitude;

    NSLog(@" ----------------------------- ");
    NSLog(@"User lat : %lf", self.userLat);
    NSLog(@"User lon : %lf", self.userLon);
    NSLog(@" ----------------------------- ");
}

I ask xCode to simulate my position to London with the Scheme option that follows :

My new load function :

-(void)viewDidLoad {
    [super viewDidLoad];
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    self.locationManager.headingFilter = 1;
    self.locationManager.delegate = self;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        [self.locationManager requestWhenInUseAuthorization];

    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    [self.locationManager startUpdatingLocation];
}

This code still doesn't print the NSLog that was put in the location callback function. Did i miss something?

Upvotes: 0

Views: 844

Answers (1)

dan
dan

Reputation: 9825

The location manager doesn't instantly have the user's location when you start updating it. If you check the location property right after calling startUpdatingLocation it is usually going to be nil unless some other app was recently updating it. You need to implement the locationManager:didUpdateLocations: delegate method which will be called once it has a location.

You also need to check and possibly request for location permissions.

Upvotes: 1

Related Questions