CLLocationManager Delegate methods are not getting called - ios

I am using CLLocationManager class. I have a simple class method for capturing the location
+(void)captureLocation{
mLocationManager = [[CLLocationManager alloc]init];
mLocationManager.delegate = (id<CLLocationManagerDelegate>)self;
mLocationManager.desiredAccuracy = kCLLocationAccuracyBest;
[mLocationManager startUpdatingLocation];
}
and i have the delegate methods of CLLocationManager also
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
}
now i am trying to call this method in my viewDidLoad as
- (void)viewDidLoad
{
[super viewDidLoad];
[myclass captureLocation];
}
the method is getting called but the delegate methods are not getting called.
I have other class method also and from there if I try to call the method again the captureLocation method is getting called but the delegate methods are not called. Here is the other class method
+(void)initialize{
[self captureLocation];
}
please help me to find out why delegate methods are not getting called as I am new to this field. Thanks in advance.

Also know that CoreLocation permissions have changed with iOS 8. If you don't request the new permissions authorizations, CoreLocation doesn't do anything. It fails quietly because the delegate methods are never called.
I realize this question was asked in 2013, but if you are having a similar problem with the delegate methods not getting called, this article is extremely helpful:
http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
While the article is very detailed and long, the actual code fix can be as minor as this:
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
And you have to add a value to info.plist, which is the message to display in the permissions alert. See screen grab.
Key: NSLocationWhenInUseUsageDescription
Value: Location is required to find out where you are (You can change that to whatever you want)

You are setting the delegate of the CLLocationManager inside a class method (i.e. one prefixed by + rather than -). So, when you reference self within that class method, that's the class, not an instance of the class. So, you are trying to set the delegate to the class rather than an instance of the class.
That won't work. The delegate methods are instance methods, not class methods. (This is presumably why you had to use the CLLocationManagerDelegate cast when assigning the delegate.)
You must actually instantiate whichever class you've implemented the CLLocationManagerDelegate methods. If you don't want to tie that instance to a particular view controller, you could use a singleton pattern. Regardless, you can set the location manager's delegate to point to that instance of that class.
For example, if you wanted it to be a singleton:
// MyClass.h
#import <Foundation/Foundation.h>
#interface MyClass : NSObject
+ (instancetype)sharedManager;
- (void)startCapture;
- (void)stopCapture;
#end
and
// MyClass.m
#import "MyClass.h"
#import <CoreLocation/CoreLocation.h>
#interface MyClass () <CLLocationManagerDelegate>
#property (nonatomic, strong) CLLocationManager *locationManager;
#end
#implementation MyClass
+ (instancetype)sharedManager
{
static id sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (void)startCapture
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}
- (void)stopCapture
{
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// ...
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// ...
}
#end
And then,
- (void)viewDidLoad
{
[super viewDidLoad];
[[MyClass sharedInstance] startCapture];
}

Calling self in a + method set your delegate to nil as it means ClassName as in [[ClassName alloc] init].
you need to:
mLocationManager.delegate = mLocationManager
instead of
mLocationManager.delegate (id<CLLocationManagerDelegate>)self;

in ios6 locationManager:didUpdateToLocation:fromLocation: is deprecated so you need to add another method in your code ...
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// this method get called in ios7 .
}

In iOS -8 need to do some changes :
Please have a look into this :Get current location in iOS-7 and iOS-8

Related

Locationmanager startmonitoringforregion not calling delegate methods

I'm trying to make a region based reminder. I have a viewcontroller that pops up when I need to add the reminder. In that vc I select a region where I need to be reminded and then use startMonitoringForRegion method. I set the locationManager delegate to the AppDelegate so that the AppDelegate can respond to entering or exiting the region.
The problem is that the when I close the viewcontroller the delegate methods don't get called. What am I doing wrong?
Here is the code:
AddReminderVC
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:self.lastCenter radius:self.radius identifier:#"id"];
[self.locationManager startMonitoringForRegion:region];
AppDelegate
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region {
NSLog(#"EXIT REGION");
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(#"ENTER REGION");
}
Note that the methods do get called when the AddReminderVC is still visible. Only when it gets dismissed the delegate methods don't work.
you have to instantiate the locationManager in your AppDelegate or write another Singleton class to hold the locationManager. If you set it in a viewController the arc will delete the object when there are no more references to it.

location delegate not firing

I'm trying to get the user location. I added the coreLocation framework, and implamented the start location method and the delegate.
The problem that the delegate function just doesn't fired.
LocationManager.h :
#interface LocationManager : NSObject<CLLocationManagerDelegate>
#property(strong,nonatomic)CLLocationManager *locationManager;
-(void)startLocaitonService;
#end
LocationManager.m :
#implementation LocationManager
-(void)startLocaitonService
{
_locationManager=[[CLLocationManager alloc]init];
_locationManager.delegate = self;
_locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
}
#end
I'm have a breakpoint in the delegate function but nothing happens
That delegate method was deprecated in iOS 6.0.
From the class reference:
Deprecation Statement
Use locationManager:didUpdateLocations: instead.

iOS: Get current location coordinates without using MKMapView

I'd like to be able to get my current user's location without actually having a map view on my view controller.
At the moment I do have a map view and get the user location by calling one of the delegate methods....
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
userlatitude = userLocation.location.coordinate.latitude;
userlongitude = userLocation.location.coordinate.longitude;
}
CLLocationManager is the class that is responsible for keeping user's location values. CLLocationManagerDelegate is another class that gets real time location data from iDevice's GPS and notifies CLLocationManager instance about the change in location and various other events, via it's delegate methods. It would be very helpful if you would read the related documentation.
You must implement CLLocationManagerDelegate protocol inside your class.
You must also have CLLocationManager instance within your class that should monitor the location.
In your project, you must also add Core Location framework in Link Binaries section.
The simplest way would be:
Your .h file:
#interface MyViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocation * currentLocation;
CLLocationManager * locationManager;
}
#end
Your .m file:
- (void) viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
//remember to stop before you are done, either here or in view disappearance.
- (void) dealloc
{
[locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
currentLocation = (CLLocation *)[locations lastObject];
}
Easily, use CCLocationManager instead of the MKMapKit::userLocaiton method.
Take a look at the "LocateMe" sample project on developer.apple.com.

call CLLocationManager and its delegates from a model

I'm trying to put everything location related inside a model. When I call this my MainViewController, the simulator doesn't ask me for my location, and nothing happens.
When I use the same code from my model, but put it directly in ViewDidLoad in my ViewController, everything works. I'm having a hard time understanding why.
Here is my model:
#implementation Location
{
CLLocationManager *_locationManager;
CLLocation *_location;
}
- (void)startLocationManager
{
NSLog(#"In startLocationManager");
_locationManager = [[CLLocationManager alloc] init];
_locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
_locationManager.delegate = self;
[_locationManager startUpdatingLocation];
}
#pragma mark - LocationManager Delegates
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"In didUpdateLocations");
if (locations) {
_location = [locations lastObject];
NSLog(#"%#", _location);
}
}
#end
I call this in my MainViewController like this:
- (void)viewDidLoad
{
[super viewDidLoad];
Location *location = [[Location alloc] init];
[location startLocationManager];
}
Why does the code work like a charm directly in the viewController, but not through my model?
I figured it out.
I needed to make location a property of your view controller instead of a local variable in the viewDidLoad. Otherwise it is created and deallocated within that method. I need it to live through the lifecycle of my view controller.

App runs fine without method, but I thought it required the method to function properly

I have a location app that had the following method in the header of the ViewController.m file for the past couple of days:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
However, I just deleted the above out of my code and the app still runs 100% fine. I don't understand how this is possible when the xcode documentation clearly says that the purpose of this method is to "tell the delegate when new location data is available."
The only thing I can think of is that it says "new location data" and that the above method is already setup in the CoreLocation.h file that I imported, and therefore already available for my use and has already stored the data.
Just want to make sure I understand the theory behind all of this before I move on.
Thank you for the help in clearing this up.
Here is my entire ViewController.m code(with the method still included):
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
#interface ViewController () <CLLocationManagerDelegate>
//This tells the delegate that new location data is available. Manager is the object that updates the event, and the locations object is where the array of location data is stored.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.gpsLM = [[CLLocationManager alloc]init];
NSLog(#"Location services enabled: %u",[CLLocationManager authorizationStatus]);
[self.gpsLM startUpdatingLocation];
self.gpsLM.delegate = self;
CLLocation * currentLocation = self.gpsLM.location;
NSLog(#"Your current location is: %#", currentLocation);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(IBAction)gpsButton{
CLLocation * currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:#"Your Location is %#", currentLocation];
NSLog(#"Location services enabled: %u",[CLLocationManager authorizationStatus]);
NSLog(#"Your current location is: %#", currentLocation);
}
#end
iOS checks if your class is capable of receiving the data with something like:
if ( [delegate respondsToSelector:#selector(didUpdateLocations:)] ){
[delegate performSelector:#selector(didUpdateLocations:) withArgs:....]
}
So if your delegate doesn't implement the method, then it doesn't attempt to send it.

Resources