How to get debugging GPS coordinates using a simulator in iOS?

I am trying to get the coordinates of my location.

I am stackoverflowed, so I am encoded below:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];

CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];

self.longitude=coordinate.longitude;
self.latitude=coordinate.latitude;

NSLog(@"dLongitude : %f",self.longitude);
NSLog(@"dLatitude : %f", self.latitude);

But I always get 0 all the time. Incorrect code? or didn’t I set my sim for GPS location?

I don’t understand why I am having problems getting coordinates.

+4
source share
3 answers

Firstly, here are some problems in your code:

  • You must save the locationManager somewhere for it to work.
  • Implement methods -locationManager:didUpdateLocations:and -locationManager:didFailWithError:delegations
  • In addition, if you are on iOS 8, you must add [locationMamager requestWhenInUseAuthorization];or[locationMamager requestAlwaysAuthorization];
  • NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription Info.plist

Xcode, Apple: https://developer.apple.com/library/ios/recipes/xcode_help-debugger/articles/simulating_locations.html

+6

[CLLocationManager location] , , :

, .

. , CLLocationManager . :

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    //User your location...
}

documentation.

+1
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    BOOL shouldIAllow = FALSE;
    NSString* locationStatus = @"";

    switch (status) {
        case kCLAuthorizationStatusRestricted:
            locationStatus = @"Restricted Access to location";
            break;
        case kCLAuthorizationStatusDenied:
            locationStatus = @"User denied access to location";
            break;
        case kCLAuthorizationStatusNotDetermined:
            locationStatus = @"Status not determined";
        default:
            locationStatus = @"Allowed to location Access";
            shouldIAllow = TRUE;
            break;
    }

    if (shouldIAllow) {
        NSLog(@"Location to Allowed");
        // Start location services
        [_locationManager startUpdatingLocation];
    } else {
        NSLog(@"Denied access: \(locationStatus)");
    }
}
0

All Articles