check if app go background

hi, is it possible detect if current uiview go in standby (for instance when you press home button or power button)? i need to disconnect a websocket before go in standby

Replies

That's not only the view, that's the whole app which goes to background.


You have func in AppDelegate where you can disconnect web socket:


    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }


applicationDidEnterBackground is probably the best place.

i try but not called.. i'm using objectc

i solve with


   [[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(yourUpdateMethodGoesHere:)
        name:UIApplicationWillEnterForegroundNotification
      object:nil];
   

In objc:

- (void)applicationDidEnterBackground:(UIApplication *)application;


with important discussion on how to save data in this context :

Discussion

Use this method to release shared resources, invalidate timers, and store enough app state information to restore your app to its current state in case it is terminated later. You should also disable updates to your app’s user interface and avoid using some types of shared system resources (such as the user’s contacts database). It is also imperative that you avoid using OpenGL ES in the background.

Your implementation of this method has approximately five seconds to perform any tasks and return. If you need additional time to perform any final tasks, you can request additional execution time from the system by calling

beginBackgroundTaskWithExpirationHandler:
. In practice, you should return from
applicationDidEnterBackground:
as quickly as possible. If the method does not return before time runs out your app is terminated and purged from memory.

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in

applicationDidEnterBackground:
will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call
beginBackgroundTaskWithExpirationHandler:
and then run the task on a dispatch queue or secondary thread.

The app also posts a

UIApplicationDidEnterBackgroundNotification
notification around the same time it calls this method to give interested objects a chance to respond to the transition.

For more information about how to transition gracefully to the background, and for information about how to start background tasks, see App Programming Guide for iOS.



Don't forget to close thread when solution found.