canaanmckenzie
canaanmckenzie

Reputation: 57

CLLocation Manager startUpdatingLocation not starting

I have been following this tutorial http://www.devfright.com/ios-6-core-location-tutorial/ to add location services to my ios app. I have imported the Corelocation in my TestFileViewController.h file like so:

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

 @interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate>

 @property (nonatomic, strong) CLLocationManager *locationManager;

 @end

and then in the testFileViewController.m ViewDidLoad I added (remembering to implement the CLLocationManagerDelegate):

//start checking for location from CoreLocation Framework (currently not working)
if ([CLLocationManager locationServicesEnabled])
{
    NSLog(@"location services enabled");
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.delegate = self;
    [_locationManager startUpdatingLocation];
};

}

(conditional CLLocationManager executes true and NSLogs) and then right below ViewDidLoad:

    //fail to find location, handle error
    - (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];
      }

      //show found location, return NSLOG for proof
      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:            (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
      {
      NSLog(@"Made it to location Manager");
      CLLocation *location = [locationManager location];

      CLLocationCoordinate2D coordinate = [location coordinate];

      NSLog(@"%f",coordinate.latitude);
      }

I added the NSLogs just as markers to determine from the console whether or not the functions were being called at all, however neither didFailWithError nor didUpdateToLocation are called.

Yes I know that didUpdateToLocation is deprecated/outdated, and I replaced it with didUpdateLocations and it still did not work so I changed it back.

How can I get my my locationManager to start updating the current location and return coordinate values in the form of strings to be used later in my app? Thanks.

Upvotes: 2

Views: 688

Answers (2)

Aswin Sathyan
Aswin Sathyan

Reputation: 283

If your are using IOS 8, you should do few things, inorder to make CLlocationManager working. one do you have NSLocationAlwaysUsageDescriptionadded to your apps info.plist file? Two have you set a location in the simulator? (Debug->Location). Third one you must request additional authorisation to use core location in ios8.

if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
    [locationManager requestAlwaysAuthorization];

Upvotes: 0

hackerinheels
hackerinheels

Reputation: 1141

Make your class a delegate for CLLocationManagerDelegate.

@interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate, **CLLocationManagerDelegate**>

And then set the delegate to self.

I would also recommend creating a separate class to handle location. Cleaner design.

Also ensure that you are calling startUpdatingLocation on the main thread.

dispatch_sync(dispatch_get_main_queue(), ^{ 

});

Upvotes: 1

Related Questions