2

I want to get my location in background every X mins, I'm using a thread which is running well with NSLog, it prints a string. However, it seems that it's not working when I call my locationManager.

This is my code:

- (void)threadEntryPoint:(ActualizarPosicionGPS2AppDelegate *)paramSender{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    while([[NSThread currentThread] isCancelled] == NO){
        [NSThread sleepForTimeInterval:10.0f];
        if ([[NSThread currentThread] isCancelled] == NO &&
            [[UIApplication sharedApplication] backgroundTimeRemaining] != DBL_MAX) {
            [self getLocation];
        }
    }

    [pool release];
}

in application:didFinishLaunchingWithOptions:

[NSThread detachNewThreadSelector:@selector(threadEntryPoint:) toTarget:self withObject:self];

Then, I've got the following:

-(void) endTaskWidthIdentifier:(NSNumber *)paramIdentifier{
    UIBackgroundTaskIdentifier identifier = [paramIdentifier integerValue];
    [[UIApplication sharedApplication] endBackgroundTask:identifier];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
        NSNumber *backgroundTask = [NSNumber numberWithInteger:self.backgroundTaskIdentifier];
        [self performSelectorOnMainThread:@selector(endTaskWidthIdentifier:) withObject:backgroundTask waitUntilDone:YES];
        self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    }];

}

And the important thing, here's getLocation:

-(void)getLocation{
    if(nil==locationManager)
        locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];
    [locationManager setDistanceFilter:kCLLocationAccuracyHundredMeters];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    [locationManager startUpdatingLocation];
    NSLog(@"ouch");
    if(locationManager.location!=nil){
        NSLog(@"obtenido %f, %f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude);
    }
}

locationManager.location always returns nil so it's not getting the current location.

Am I doing anything wrong? Maybe I have a wrong concept!

Thank you very much!

Ibaivi
  • 51
  • 4
  • Have you implemented the CLLocationManagerDelegate methods? –  Jul 07 '11 at 23:54
  • Which methods? I just have this: - (void)locationManager didUpdateToLocation: fromLocation: . And added CLLocationManagerDelegate to the .h – Ibaivi Jul 08 '11 at 09:33
  • Does didUpdateToLocation get called? Also add `didFailWithError` to see if CL is failing. Don't expect `location` to have value right after calling startUpdatingLocation (if it does, will be cached value). Suggest only reading `location` in didUpdateToLocation. –  Jul 08 '11 at 12:56
  • However, not sure the approach with beginBackgroundTaskWithExpirationHandler will work way you think. Suggest re-read docs for that method, and CL, as well as "Declaring the Background Tasks You Support" and "Implementing Long-Running Background Tasks" sections in [iOS App Pgmg Guide](http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html). –  Jul 08 '11 at 12:56
  • Thank you Anna. Well, it seems that it's not going to work. didUpdateToLocation is not being called, neither didFailWithError. I'll continue looking for a solution, I want to get the location every X mins in background and send it to a server with SOAP. – Ibaivi Jul 09 '11 at 17:27

1 Answers1

1

See my answer in this post.

How do I get a background location update every n minutes in my iOS application?

You might also wanna check why your delegate methods are not getting called

If you are comfortable with it, send me your project at xs2bush@yahoo.com. Ill execute the code to see whats going wrong.

Community
  • 1
  • 1
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37