Search code examples
swiftappdelegateios13xcode11uiscenedelegate

Difference between "didBecomeActive" and "willResignActive" method pairs for UISceneDelegate and UIApplicationDelegate?


My main task is to overlap the screen with non-transparent view to hide info when app enters background. The official solution is described here.

The problem is it sometimes doesn't work on iOS 13.

I found this article:

It explains how sceneDidBecomeActive(_:) and sceneWillResignActive(_:) work. But new projects have AppDelegate+SceneDelegate, old ones have AppDelegate only. Could I just use applicationDidBecomeActive(_:) and applicationWillResignActive(_:)? Or should I somehow migrate the project to use UISceneDelegate (which is not supported in iOS 12 and earlier versions)?


Solution

  • If you have an app created prior to Xcode 11 then it has no support for scenes. As long as you don't add anything related to scenes (which Apple may disallow in the future), then your app's lifecycle will continue to work under iOS 13 just as it always has under iOS 12 and earlier. Just continue to use the UIApplicationDelegate methods.


    If you have an app that supports iOS 12 and iOS 13 and you are using scenes in iOS 13, then you must implement both applicationWillResignActive (in support of iOS 12 and earlier) and sceneWillResignActive (in support of iOS 13 and later).

    In fact, in an app that supports both iOS 12 and 13 you basically need to implement both sets of UIApplicationDelegate and UISceneDelegate/UIWindowSceneDelegate methods. The methods of UIApplicationDelegate will be called when your app runs on a device with iOS 12 or earlier and the UISceneDelegate/UIWindowSceneDelegate methods will be called when your app runs on a device with iOS 13 or later.

    Just remember that under iOS 13, if you support multiple windows/scenes, the scene delegate methods are associated with a specific scene.