0

my program is working fine in iOS7 but not iOS8.. locationManager is object of CLLocationManager here.

In starting this code is working fine in iOS7,i can not understand why this happen to me.

- (void)getCurrentLocation{
    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if (status == kCLAuthorizationStatusAuthorized) {

        NSUserDefaults *notify = [NSUserDefaults standardUserDefaults];
        [notify setObject:@"GPS" forKey:@"Notification"];
        [notify synchronize];

        appDelegate = (JobDiagnosisAppDelegate *)[[UIApplication sharedApplication] delegate];
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
        [appDelegate showIndicator];
        [notify setObject:nil forKey:@"Notification"];
        [notify synchronize];
    }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    [appDelegate.objActivityAlertView close];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];

    appDelegate = (JobDiagnosisAppDelegate *)[[UIApplication sharedApplication] delegate];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
    [appDelegate showIndicator];
    [self callWebserviceLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation: locationManager.location completionHandler:
     ^(NSArray *placemarks, NSError *error) {

         //Get address
         CLPlacemark *placemark = [placemarks objectAtIndex:0];

         //String to address
         NSString *locatedaddress =[placemark valueForKey:@"administrativeArea"] ;
         //Print the location in the console
         NSLog(@"Currently address is: %@",locatedaddress);

         NSUserDefaults *notifyLocation = [NSUserDefaults standardUserDefaults];
         [notifyLocation setObject:locatedaddress forKey:@"notifyLocation"];
         [notifyLocation synchronize];

         locationManager.delegate = nil;
         [self callWebserviceUnRegistered];

     }];
    [locationManager stopUpdatingLocation];

}

please help to solve this location problem.I am new in location services.

thanks in advance!!

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
Jason
  • 151
  • 1
  • 13
  • possible duplicate of [iOS 8 : Location Services not working](http://stackoverflow.com/questions/24062509/ios-8-location-services-not-working) – Paulw11 Oct 08 '14 at 11:52

2 Answers2

0

You Need to implement [locationManager requestWhenInUseAuthorization];or

[locationManager requestAlwaysAuthorization];

If you do not make either of two request , iOS will ignore startUpdateLocation request.

Also,

Include NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist depending upon which permission you are asking for. This string will be diaplayed by iOS to user so the user can get excat idea why does our app needs permission.

So change your methood to

- (void)getCurrentLocation{
    locationManager = [[CLLocationManager alloc] init];
    geocoder = [[CLGeocoder alloc] init];
    locationManager.delegate = self;
    [locationManager requestWhenInUseAuthorization];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
}

And aslo add required key in info.plist.

Hope this helps.

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
0

First, don't forget, with IOS 8 you need to add NSLocationWhenInUseUsageDescription property to the info.plist file in order to prompt the alert to get the user's permission and use[locationManager requestWhenInUseAuthorization] plus [locationManager requestAlwaysAuthorization];

_locationManager = [CLLocationManager new];

if(SYSTEM_IS_OS_8_OR_LATER) {
    [_locationManager requestWhenInUseAuthorization];
    [_locationManager requestAlwaysAuthorization];

}

_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager startUpdatingLocation];

and in your didUpdateLocations

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

        _currentLocation = [locations objectAtIndex:0]; 
        //do your stuff with the location

    }
Smiless
  • 422
  • 2
  • 10
  • how to add NSLoationWhenInUseUsageDescription property to the info.plist file? – Jason Oct 09 '14 at 11:38
  • select the targets >> info tab >> add "NSLocationWhenInUseUsageDescription" as string and the value is a submessage in the permission dialog .(so not really needed) – Smiless Oct 09 '14 at 13:15