Achintha Gunasekara
Achintha Gunasekara

Reputation: 1165

iOS location issue - can't see any data coming in

I'm new to iOS programming and trying to get my current location. My main app delegate is like this,

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@class AWSViewController;

@interface AWSAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) AWSViewController *viewController;
@property (strong, nonatomic) UINavigationController *navController;
@property (strong, nonatomic) NSMutableArray *cities;

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

@end

And in the implementation, i have these 2 methods and i call getCurrentLocation from (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

I can see the words "Started to get locations" on the console and I don't see anything else after.

-(void)getCurrentLocation {

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    locationManager.distanceFilter = 500;
    [locationManager startUpdatingLocation];
    NSLog(@"Started to get locations");
}

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

    CLLocation *location = [locations lastObject];
    NSLog(@"Got Location");
    NSLog(@"latitude %+.6f, longitude %+.6f\n", location.coordinate.latitude, location.coordinate.longitude);
}

What am I doing wrong here? thanks

Upvotes: 1

Views: 122

Answers (3)

nevan king
nevan king

Reputation: 113757

Your location manager goes out of scope as soon as the code exits your getCurrentLocation method. Make a location manager ivar so that it stays around.

@interface AWSAppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}



-(void)getCurrentLocation {
    locationManager = [[CLLocationManager alloc] init];
    // etc
}

Upvotes: 3

Achintha Gunasekara
Achintha Gunasekara

Reputation: 1165

I fixed it :D

my locationManager object - I instantiate it but you don't retain so it vanishes instantly in a puff of smoke

@property (strong, nonatomic) CLLocationManager *locationManager;

Then,

-(void)getCurrentLocation {

    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
    self.locationManager.distanceFilter = 5;
    [self.locationManager startUpdatingLocation];
    NSLog(@"Started to get locations");
}

Now it works perfectly :D

Upvotes: 1

Alberto Vitale
Alberto Vitale

Reputation: 71

Is your location manager enabled? Check it:

if(locationManager.locationServicesEnabled == NO){
  //go to settings>location services
}

If something is going bad, the method

locationManager:didFailWithError:

is called instead of

locationManager:didUpdateLocations

so, please, implement it:

- (void)locationManager: (CLLocationManager *)manager didFailWithError: (NSError *)error {
    NSLog(@"error%@",error);
}

Upvotes: 0

Related Questions