0

I use Following Code and i set delegate also but when i click Action button DidupdateLocation and DidfailError Events never call it.Please help me to find my mistake.

 - (IBAction)onUseCurrentLocation:(id)sender {
        locationManager = [[CLLocationManager alloc] init];
        [locationManager setDelegate:self];
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
         if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
            [locationManager requestWhenInUseAuthorization];
            [locationManager startUpdatingLocation];
       }

    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
    {
        NSLog(@"didFailWithError: %@", error);
        UIAlertView *errorAlert = [[UIAlertView alloc]
                                   initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [errorAlert show];
    }

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

        if (currentLocation != nil) {
            NSLog(@"Longitude:-%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]);
            NSLog(@"Latitude:-%@",[NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]);

        }
    }
imjaydeep
  • 878
  • 1
  • 11
  • 34

1 Answers1

1

One of the solution currently I found

Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

But even if you implement one of the above methods, it won't prompt the user unless there is an entry in the Info.plist for NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription.

Add the following lines to your Info.plist (obviously with the message you want your users to see :),

<key>NSLocationWhenInUseUsageDescription</key>
<string>The spirit of stack overflow is coders helping coders</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>I have learned more on stack overflow than anything else</string>
rmaddy
  • 314,917
  • 42
  • 532
  • 579
imjaydeep
  • 878
  • 1
  • 11
  • 34