0

I have been trying to fake my location in the iOS simulator but the methods I have been reading about are not working. I have used the debugger to set my fake location, and have made a custom location in Debug->Location->Custom Location, yet I still log 0.000 0.000 for lat and long. I'm using this code to find current location.

- (IBAction)currentLocationButtonPressed:(UIButton *)sender
{
    //Search for pubs and bars in current location.
    //Push to tableViewController
    NSLog(@"current location Pressed.");

    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; // Best accuracy
    [locationManager startUpdatingLocation];
    NSLog(@"%@",[self deviceLocation]);
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        NSLog(@"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    }
}

- (NSString *)deviceLocation {
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}

The delegate is set and I am requesting authorization.

- (void)viewDidLoad {
    [super viewDidLoad];

    searchLocationTextField.delegate = self;
    locationManager.delegate = self;
    locationManager = [[CLLocationManager alloc]init];
    [locationManager requestWhenInUseAuthorization];  // For foreground access
    [locationManager requestAlwaysAuthorization]; // For background access

}
Andy
  • 750
  • 7
  • 23

1 Answers1

0

Set locationManager delegate to after allocating it.

locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;

locationManager:didUpdateToLocation:fromLocation: delegate method is deprecated, use locationManager:didUpdateLocations: instead.

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations lastObject];

    NSLog(@"Latitude: %f Longitude: %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
} 
Asif Asif
  • 1,511
  • 14
  • 24
  • Yeah, that seems to make more sense, but NSLog(@"didUpdateToLocation: %@", [locations lastObject]); wont even log to the console. – Andy Oct 18 '14 at 15:26
  • Even after update, currentLocation is not logged to the console. it seems as though the method isnt even getting called. – Andy Oct 18 '14 at 15:34
  • Also, you need to set delegate after allocating the locationManager, not the other way. Answer updated. – Asif Asif Oct 18 '14 at 15:37
  • I thought that'd be it. Still not getting logged to the console. – Andy Oct 18 '14 at 15:40
  • do you have a device to test on? The location delegate does not always get fired in simulator. I am used to this behavior since Xcode 5. – Asif Asif Oct 18 '14 at 15:44
  • No, I can only use the simulator right now. You're saying you think its just a bug? I guess for now I will just stub the current location. – Andy Oct 18 '14 at 15:47
  • btw, I noticed the call to both `requestWhenInUseAuthorization` and `requestAlwaysAuthorization`. Have you added the keys `NSLocationAlwaysUsageDescription` and `NSLocationWhenInUseUsageDescription` to the info.plist? – Asif Asif Oct 18 '14 at 15:51
  • I just did. I got this error. 2014-10-18 16:59:48.430 YelpTest2[9899:685126] Location manager failed to find loction with error:Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)" – Andy Oct 18 '14 at 16:01
  • related solution: http://stackoverflow.com/questions/1409141/location-manager-error-kclerrordomain-error-0 – Asif Asif Oct 18 '14 at 16:04