Getting location in iOS background
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!
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.
精彩评论