Ann
Ann

Reputation: 13

CLLocationManager fails to get the user's location in device but not in simulator

I'm using a CLLocationManager for a GPS tracking system in my iOS app. The app works fine with the debug feature of the simulator but once the build is in the device, it fails to do so. Giving me an error, "Failed to get location." This is how I did it:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.pausesLocationUpdatesAutomatically = NO;

then the startupdating is in a uibutton. Any help will be gravely appreciated. Thank you.

Upvotes: 1

Views: 1329

Answers (1)

Kanhaiya Sharma
Kanhaiya Sharma

Reputation: 1038

First setup the location manager

 locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
        [locationManager startUpdatingLocation];

And after that call delegate method

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);

    currentLocationCoordinate.latitude = location.coordinate.latitude;
    currentLocationCoordinate.longitude = location.coordinate.longitude;

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Address Found" object:self];

}

this is work for me . it will help you

Upvotes: 1

Related Questions