3

I'm making an iPhone application that uses the user location, however I've noticed that when the iPhone screen sleeps/turns off, the phone is still using user location. So is there a function that can tell me when the screen sleeps so i can stopUpdatingLocation?

@interface RideauAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
    UIWindow *window;
    UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end
thechengster
  • 133
  • 1
  • 10

1 Answers1

5

You can use:

- (void)applicationWillResignActive:(UIApplication *)application {
    [[[self.tabBarController.viewControllers objectAtIndex:0] locationManager] stopUpdatingLocation]; 
    //if it is your first VC, if not change objectAtIndex: to the correct one

}
Nicolas S
  • 5,325
  • 3
  • 29
  • 36
  • 3
    Alot of useful information already covered in Stack Overflow topic here: http://stackoverflow.com/questions/411436/what-happens-to-an-iphone-app-when-iphone-goes-into-stand-by-mode – Perception Jun 20 '11 at 18:53
  • This is only a function in AppDelegate. If I used this how would I access my CLLocationManager variable in a different controller so i can call [locationManager stopUpdatingLocation]; ? – thechengster Jun 20 '11 at 20:00
  • Is your controller a property of your delegate? – Nicolas S Jun 20 '11 at 20:01
  • It's part of my tabBarController which is a property of my delegate – thechengster Jun 20 '11 at 20:06
  • Is your tabBarController declared in your delegate? So your controller should be instanced there too? – Nicolas S Jun 20 '11 at 20:08
  • sorry im fairly new at this and don't really know what you mean. like i just started with the default tabView App template, and added tabs in the main nib file where i connect those to my mapView.xib and my MapViewController – thechengster Jun 20 '11 at 20:11
  • OK, dont worry, let me help you by posting your AppDelegate.h file – Nicolas S Jun 20 '11 at 20:15
  • #import @interface RideauAppDelegate : NSObject { UIWindow *window; UITabBarController *tabBarController; } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; @end – thechengster Jun 20 '11 at 20:17
  • i have to go for like 30 minutes. but any help would be appreciated. thanks.. and sorry it didn't look nice when i posted my appDelegate.h – thechengster Jun 20 '11 at 20:19
  • post it in your main Question, it can be better formated there. – Nicolas S Jun 20 '11 at 20:20
  • Where is the ViewController declared? or it is just set up in Interface Builder? – Nicolas S Jun 20 '11 at 21:20
  • ya it is all set up in interface builder – thechengster Jun 20 '11 at 21:38
  • Well then it is a bit difficult to do it right, without adding too much code, try doing what ill update in my answer. – Nicolas S Jun 20 '11 at 21:42
  • well if theres a proper way, i can change what i have – thechengster Jun 20 '11 at 21:44
  • Ok, that was the _adding too much code_ i told you earlier.. Great to hear you worked it out – Nicolas S Jun 21 '11 at 14:35