didUpdateLocation Method Never Called
Asked Answered
F

6

7

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];
}
Forsterite answered 21/3, 2011 at 5:40 Comment(1)
finally it worked...i didn't do anything.i just made one new project and copied all classes in it.i don't know how?but it worked..Forsterite
R
5

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.

Reward answered 21/3, 2011 at 6:17 Comment(0)
C
18

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/

Cloth answered 22/9, 2014 at 16:32 Comment(1)
… and in general the Always methods are discouraged, use the others if you're not sure what you're doing.Hying
R
5

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.

Reward answered 21/3, 2011 at 6:17 Comment(0)
G
4

Make sure you added CLLocationManager as a property.

@property (nonatomic , strong) CLLocationManager *locationManager;

Germanism answered 28/1, 2014 at 17:39 Comment(3)
Gabriel you have showed me the good way.I got the solution now.I tried to call the didupdateLocation method but it did not call even though I applied everything except your suggestion.Finally I applied CLLocationManager as a property.Now the delegate method is called and I get the update location(Lat,long) value.Capacity
Could you please tell me the reason or explanation about property CLLocationManager and why does not work without propert?Capacity
Because if it is a property it have an strong reference. Without being a property it gets deallocated outside the method. So it can not receive the callbacks from the delegate. @CapacityGermanism
E
3

If you are testing this on the simulator it will not work.

This happens because that method is only called when the device changes location (and the simulator never changes location).

Evelineevelinn answered 21/3, 2011 at 7:9 Comment(3)
No..I am not checking on Simulator..I am checking it on iphone deviceForsterite
Have you tried calling for the current location manually and making sure you have a valid location? You can do this using locationManager.location.Evelineevelinn
This is not true, you can simulate movement now. Simulator -> Debug -> LocationTitanomachy
S
1

Does -locationManager:didFailWithError: in your delegate ever get called? Maybe you denied access to location data at some point and now don't get prompted, but access is denied.

Sympathizer answered 21/3, 2011 at 6:9 Comment(0)
D
0

After you alloc and init your location manager, check to see if location services are allowed. This code is from Apple's "LocateMe" code sample

if (locationManager.locationServicesEnabled == NO) {
    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [servicesDisabledAlert show];
    [servicesDisabledAlert release];
}

If you're testing with the simulator, try testing with a device instead. The simulator requires that the computer's WiFi is turned on, and that it can locate a WiFi base station that's in it's database of known locations.

Daze answered 21/3, 2011 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.