Search code examples
iphonescreensleepuserlocation

iPhone how to tell application when iPhone screen sleeps


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

Solution

  • 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
    
    }