How to work with SceneDelegate in iOS 12
Issue #580
Events
open url
Implement scene(_:openURLContexts:) in your scene delegate.
If the URL launches your app, you will get scene(_:willConnectTo:options:) instead and it’s in the options.
life cycle
Here’s how it works: If you have an “Application Scene Manifest” in your Info.plist and your app delegate has a configurationForConnectingSceneSession method, the UIApplication won’t send background and foreground lifecycle messages to your app delegate. That means the code in these methods won’t run:
applicationDidBecomeActive
applicationWillResignActive
applicationDidEnterBackground
applicationWillEnterForeground
The app delegate will still receive the willFinishLaunchingWithOptions: and didFinishLaunchingWithOptions: method calls so any code in those methods will work as before.
UIApplication notifications
Notifications still trigger in iOS 13 if adopting SceneDelegate
1 | UIApplication.didBecomeActiveNotification |
Use foreground transitions to prepare your app’s UI to appear onscreen. An app’s transition to the foreground is usually in response to a user action. For example, when the user taps the app’s icon, the system launches the app and brings it to the foreground. Use a foreground transition to update your app’s UI, acquire resources, and start the services you need to handle user requests.
All state transitions result in UIKit sending notifications to the appropriate delegate object:
In iOS 13 and later—A UISceneDelegate object.
In iOS 12 and earlier—The UIApplicationDelegate object.
You can support both types of delegate objects, but UIKit always uses scene delegate objects when they are available. UIKit notifies only the scene delegate associated with the specific scene that is entering the foreground. For information about how to configure scene support, see Specifying the Scenes Your App Supports.
keyWindow
Show most recent activeUIWindow
1 | UIApplication.shared.keyWindow |
This property holds the UIWindow object in the windows array that is most recently sent the makeKeyAndVisible() message.
AppDelegate vs SceneDelegate
Get sceneDelegate from AppDelegate
1 | UIApplication.shared.openSessions.first?.scene?.delegate |
order
1 | SceneDelegate.sceneDidBecomeActive |