Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

didUpdateLocation Method Never Called

I am making one app on iphone sdk4.0.In that did update location method never called. I have given my code below.Please help.Thanks in advance.

-(id)init
{
    [super init];

    obj=[[UIApplication sharedApplication]delegate];

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

    return self;

}

-(void)locationManager:(CLLocationManager *)manager
   didUpdateToLocation:(CLLocation *)newLocation
          fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"%f",newLocation.coordinate.latitude);

    NSLog(@"%f",newLocation.coordinate.longitude);

    obj=[[UIApplication sharedApplication]delegate];

    location=[[CLLocation alloc]initWithLatitude:newLocation.coordinate.latitude longitude:newLocation.coordinate.longitude];
    obj.lattitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];

    obj.longitude1=[NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
    //location=[[CLLocation alloc]initWithLatitude:39.9883 longitude:-75.262227];
}
like image 611
Akshay Aher Avatar asked Mar 21 '11 05:03

Akshay Aher


2 Answers

Furthermore in iOS8 you must have two extra things:

  • Add a key to your Info.plist and request authorization from the location manager asking it to start.

    • NSLocationWhenInUseUsageDescription

    • NSLocationAlwaysUsageDescription

  • You need to request authorization for the corresponding location method.

    • [self.locationManager requestWhenInUseAuthorization]

    • [self.locationManager requestAlwaysAuthorization]

Code example:

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

Source: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/

like image 136
MarMass Avatar answered Oct 21 '22 20:10

MarMass


HI, your codes seems ok. now these can be possible reasons :

on your init: try to check if there is locationServicesEnabled or not.

locationManager = [[CLLocationManager alloc] init];
if(locationManager.locationServicesEnabled == NO){
    //Your location service is not enabled, Settings>Location Services  
}

other reason, you may disallow to get location to your app. solution: simply remove your application from iPhone and re-build, now it should popup dialog to Allow location.

use this to check error

- (void)locationManager:(CLLocationManager *)manager
       didFailWithError:(NSError *)error
{       
    NSLog(@"Error while getting core location : %@",[error localizedFailureReason]);
    if ([error code] == kCLErrorDenied) {
        //you had denied 
    }
    [manager stopUpdatingLocation];
}

otherwise all seems ok, you were already running ios 4.0 , which can be install in iPhone 3G and later, if it was iPhone 2g, then this problem may occur.

like image 33
Mujah Maskey Avatar answered Oct 21 '22 20:10

Mujah Maskey