Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

viewWillTransitionToSize:withTransitionCoordinator: called when app goes into the background or inactive

It appears that the method viewWillTransitionToSize:withTransitionCoordinator: gets called multiple times when the app goes into the background or inactive since iOS 9.

For example, if the app is in the portrait on iPad, pressing the home button will cause the app to receive method call first with size of 1024x768 (landscape) and then with 768x1024 (back to portrait). That leads me to conclude that iOS does this in order to get screenshots for app switcher.

Our application logic depends on the screen size and changes to the screen size trigger tasks that update our model with regard to the new size. We need to do this when user rotates the device or goes into multitasking mode (split view), but we must not do it when user goes into the background.

One idea was to use UIApplicationWillResignActiveNotification notification, but that turned out to be mission impossible because sometimes viewWillTransitionToSize:withTransitionCoordinator: gets called before the notification is sent and othertimes it gets called after the notification is sent :/

Any ideas?

like image 568
Srđan Avatar asked Oct 07 '15 13:10

Srđan


People also ask

What is the state of an app that is running in the foreground but is not receiving any events suspended?

Inactive – The app is running in the foreground, but not receiving events. An iOS app can be placed into an inactive state, for example, when a call or SMS message is received. Active – The app is running in the foreground, and receiving events. Background – The app is running in the background, and executing code.


2 Answers

In viewWillTransitionToSize:withTransitionCoordinator you can use the UIViewControllerTransitionCoordinator to tell when the transition is complete. Then, check that your app is not backgrounded and perform your logic.

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [coordinator animateAlongsideTransition:nil completion:^(id<UIViewControllerTransitionCoordinatorContext>  _Nonnull context) {

        // after transition

        if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground) {
            // perform logic
        }
    }];
}
like image 182
AddisDev Avatar answered Sep 19 '22 18:09

AddisDev


I currently have the same problem. I do a lot of work when the size changes and can't have it happening when going into the background. One thing that is working for me "most" of time is to check if the app is in the background using [uiapplication sharedApplication] applicationState]. The application state is set to UIApplicationStateBackground before the 2 extra calls to viewWillTransitionToSize are made. This seems to be consistent. I'm having some problems when the app is swiped in using multitasking then goes into the background and then foreground again at full size. The order of calls doesn't seem to be consistent in this case to determine which size changes should be used.

like image 42
Kathy Hicks Lawler Avatar answered Sep 18 '22 18:09

Kathy Hicks Lawler