2

I want to detecting user's current location in my app.I am using objective c.It's working fine in simulator but while testing on device below error comes.

didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)"

Please Help me to solve this issue.I am using Lat long value for find out place mark in my application.

  if(version<8.0)
{
    locationManager = [[CLLocationManager alloc] init];


    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
}
else
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
    if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];
}

Here are delegate method

- (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) {
    NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
    NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];

    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];



    [geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
     ^(NSArray *placemarks, NSError *error) {


         CLPlacemark *placemark = [placemarks objectAtIndex:0];


         NSString *placemark_str = [placemark locality];
         subAdminArea.text=placemark.subAdministrativeArea;

         NSString *are_str = [placemark subLocality];

            subAdminArea.text=placemark.subAdministrativeArea;


         NSString *location_str=[NSString stringWithFormat:@"%@,%@",are_str,placemark_str];

         [[NSUserDefaults standardUserDefaults]setValue:location_str forKey:@"Location"];

         NSLog(@"place mark str: %@",placemark_str);


     }];

}}


- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
NSLog(@"%@", [locations lastObject]);

CLGeocoder * geoCoder = [[CLGeocoder alloc] init];


[geoCoder reverseGeocodeLocation: [locations lastObject] completionHandler:
 ^(NSArray *placemarks, NSError *error) {


     CLPlacemark *placemark = [placemarks objectAtIndex:0];


     NSString *placemark_str = [placemark locality];
     NSString *are_str = [placemark subLocality];



     NSString *location_str=[NSString stringWithFormat:@"%@,%@",are_str,placemark_str];

     [[NSUserDefaults standardUserDefaults]setValue:location_str forKey:@"Location"];




 }];}




- (void)requestAlwaysAuthorization{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];

// If the status is denied or only granted for when in use, display an alert
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
    NSString *title;
    title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled";
    NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings";

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                        message:message
                                                       delegate:self
                                              cancelButtonTitle:@"Cancel"
                                              otherButtonTitles:@"Settings", nil];
    [alertView show];
}
// The user has not enabled any location services. Request background authorization.
else if (status == kCLAuthorizationStatusNotDetermined) {
    [locationManager requestAlwaysAuthorization];
}}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1) {
    // Send the user to the Settings for this app
    NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    [[UIApplication sharedApplication] openURL:settingsURL];
}}

I have also update my plist file with NSLocationAlwaysUsageDescription->String & NSLocationWhenInUseUsageDescription -> String.

Thank You.

user3418619
  • 235
  • 1
  • 2
  • 14
  • I believe you are testing this on iOS 8 device... Right? For iOS 8 something is changed... please google before asking here... – Fahim Parkar Feb 17 '15 at 06:13
  • No ios 7 device and I know about ios 8 issue with location. It's working fine in simulator for both ios 7 and 8 but not in device. Thank you Fahim Replaying me. – user3418619 Feb 17 '15 at 06:16
  • add the code also to the question..it may help to the readers to find out the problem...:) – Ravi Feb 17 '15 at 06:20
  • @user3418619 : show us the code then... would help us... – Fahim Parkar Feb 17 '15 at 06:24
  • see So answer http://stackoverflow.com/questions/6032976/didfailwitherror-error-domain-kclerrordomain-code-0-the-operation-couldn-t-be – Deepesh Feb 17 '15 at 06:50
  • Deepesh I have tested it in 3 device every one has same problem with same error. Thank You for Replaying me – user3418619 Feb 17 '15 at 06:54

1 Answers1

-1

1) check that you actually have a valid WiFi and 3G connection

if you do then

2) go to settings and reset your location services

3) then reset your network settings

Developer
  • 65
  • 3
  • I have already apply all this steps but not working in my condition.Thanks For replaying me.and tell me if you have another solution or working code – user3418619 Feb 21 '15 at 11:18