iOS How To: Check if Application is Active
Have you ever wanted to know when your app entered the background or came back into the foreground? But you don’t want to have to hassle with trying to run methods in your current view from the App Delegate? I can ease your pain. Add the following code to the viewDidLoad method of the class you like to handle these events:
// check if app is in the background [[NSNotificationCenter defaultCenter] addObserver:@"EnteredBackground" selector:@selector(appDidEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil]; // check if app is in the foreground [[NSNotificationCenter defaultCenter] addObserver:@"EnteredForeground" selector:@selector(appDidEnterForeground) name:UIApplicationDidBecomeActiveNotification object:nil];
The first one will run the appDidEnterBackground method when the app has, indeed, entered the background.
The second does the same for entering the foreground.
Comments are closed.