CLLocationManager not calling its delegate method - ios

I am developing an app which involves tracking the new location and updates to the server in X mins. I am stopping the location updating when the time diff is equal to or greater than X mins, then sending to the server and then start updating the location if it is less than X mins. But the didUpdateToLocation delegate methos is not called when it is less than X mins.
I am posting my code here:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (theDiff > 10.0f || myDate == nil)
{
[self stopUpdating];
}
else
{
[self.mLocationManager startUpdatingLocation];
}
}

try this.,
//in your .h
CLLocationManager *locationManager;
#property (nonatomic,retain) CLLocationManager *locationManager;
// Then synthesize it in your .m
#synthesize locationManager;
//in your viewDidLoad
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
This will call the method whwnever you move with your phone. You can check this by changing simulator locatin also.
Hope this helps., Thanks, happy coding.

try this in .m add this method , or if you have any other custom code post it
-(void)LocationWithTime
{
[CLController sharedInstance].locationManager.desiredAccuracy = 100.0;
[CLController sharedInstance].delegate = self;
BOOL locationAccessAllowed = NO ;
if( [CLLocationManager respondsToSelector:#selector(locationServicesEnabled)] )
{
locationAccessAllowed = [CLLocationManager locationServicesEnabled] ;
}
if(locationAccessAllowed == YES)
{
[[CLController sharedInstance].locationManager startUpdatingLocation];
}
}
this is the delegate method i have used -
-(void)newLocationUpdate:(NSString *)latvalue longValue:(NSString*)longvalue Error:(int) errCode
{
DebugLog(#"LatLong values %# %#", [UserContext sharedInstance].strLat, [UserContext sharedInstance].strLong);
self.strLALat = latvalue;
self.strLALong = longvalue;
[[CLController sharedInstance].locationManager stopUpdatingLocation];
[CLController sharedInstance].delegate=nil ;
}

Excerpt from Apple API Documentation,
/*
* locationManager:didUpdateToLocation:fromLocation:
*
* This method is deprecated. If locationManager:didUpdateLocations: is
* implemented, this method will not be called.
*/
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_6, __MAC_NA, __IPHONE_2_0, __IPHONE_6_0);
Use the below delegate method instead,
/*
* locationManager:didUpdateLocations:
* locations is an array of CLLocation objects in chronological order.
*/
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_6_0);
This will work once you set the delegate.
Hope this helps.

Related

how to call didUpdateToLocation after shake on iPhone

I'm trying get location updates when the user shakes his iPhone, I'm enabling GPS on receiving the shake event, but my problem is that the didUpdateToLocation method is not called. Can someone tell me what is wrong with my code?
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
if(event.type == UIEventSubtypeMotionShake)
{
[self first];
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
// NSLog(#"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSString * currentLat = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
NSLog(#"currentLat%#",currentLat);
NSString * currentLong = [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
NSLog(#"currentLong%#",currentLong);
}
-(void)first
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(#"called");
[self.view setBackgroundColor:[UIColor greenColor]];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self becomeFirstResponder];
}
Make sure that location-services are enabled and App has access to location-services (under iPhone Settings -> Location Services). The method you are using is deprecated since iOS 6. You should use
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
I would recommend you implementing following method as well for debugging purpose.
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
ps. I'm not sure about the scope for locationManager in your method first. Why don't you make it (strong) property and then initialise it in viewDidLoad and only call startUpdatingLocation in your method first.

Calling and releasing CLLocationManager With ARC

My app should get the user's current location only one time.
It's doing it successfully using the location manager and a delegate. Once i get the location for the first time my delegate calls the manager to stopUpdatingLocation.
Since I'm using ARC, and I keep an instance of this class during all the time my app runs, my CLLocationManager instance stays in memory.
I know GPS services are quite power consuming, but does it have the same effect while i actually don't consume more events? does it keep on working?
I want to know if i should add some logic to release it.
Try Following...
-(void)updateLoc
{
if (self.locationManager != nil)
{
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
self.locationManager = nil;
}
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.delegate = self;
self.locationManager.headingFilter = 1;
self.locationManager.distanceFilter=10.0;
[self.locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{//Use if you are supporting iOS 5
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}

iOS 8 beta - Location Manager Not Registering User Location

Since the new iOS 8 beta release, I have not been able to successfully get a user's location. Prior to the update to iOS 8 I had no issues, but now it always returns 0.000000 as the current latitude and longitude. Is this just a bug in the new release? My code is listed below:
//from the .h file
#interface MasterViewController : PFQueryTableViewController<CLLocationManagerDelegate,UITextFieldDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
}
#property (nonatomic, strong) CLLocationManager *locationManager;
//from the .m file
#synthesize locationManager = _locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
[self.locationManager startUpdatingLocation];
}
- (CLLocationManager *)locationManager {
if (_locationManager != nil) {
return _locationManager;
}
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
return _locationManager;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
}
UPDATE
This question has been answered (Location Services not working in iOS 8). For anyone still struggling with this, to maintain backwards compatibility with iOS 7, I used the code below:
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) { }
As mentioned in your update/comment, iOS8 requires you to use requestAlwaysAuthorization or requestWhenInUseAuthorization as well as a new NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist
But there is something else which is wrong in your code:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
It has been deprecated in iOS6 and you should now use this new delegate method instead:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
You can then get the latest location with something like this:
CLLocation *newLocation = [locations lastObject];

Location manager stops updating after 1 hour

According my app requirement, we do tracking on, and its working fine. but problems comes when i enable GPS service on and never moves and place iPhone on desk for 1 hour after that i do journey of 30 minutes but my update location delegate never called this time.below is my code:
locationManager = [[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = 150.0f;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0")) {
setPausesLocationUpdatesAutomatically:NO];
}
Write this code :
[locationManager startUpdatingLocation];
& write their delegates methods also :
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSMutableString *latString = [NSMutableString stringWithFormat:#"%f",newLocation.coordinate.latitude];
NSMutableString *lngString = [NSMutableString stringWithFormat:#"%f",newLocation.coordinate.longitude];
}

Location Manager is not updating location

I have the following code:
- (void) viewWillAppear {
[self startSignificantChangeUpdates];
}
- (void)startSignificantChangeUpdates
{
if ([CLLocationManager locationServicesEnabled])
{
NSLog(#"start significant changes");
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
}
problem is that the location manage is not calling its delegate function
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
What can I do?
In your .h file:
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
In your .m file:
#implementation ViewController {
// This is a private variable, used within this file only
CLLocationManager *locationManager;
}
-(void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
}
-(void)viewWillAppear {
// If the delegate is still not being set, try putting this code into viewDidAppear
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"didFailWithError: %#", error);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(#"didUpdateToLocation: %#", [locations lastObject]);
CLLocation *currentLocation = [locations lastObject];
[locationManager stopUpdatingLocation];
}
For more information about getting the user's location, look at this guide, which I used to implement location into an app of mine: http://www.appcoda.com/how-to-get-current-location-iphone-user/
How are you testing this? If you are running this in the simulator, you can go to Debug->Location->Freeway Drive. You can put a break point in locationManager:didUpdateToLocation method or even put a log statement to log the current location coordinates. You should see it work.
Please replace this line
[locationManager startMonitoringSignificantLocationChanges];
with this code
[locationController.locationManager startUpdatingLocation];
and then check
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
}

Resources