Tapping a WidgetKit widget automatically launches its parent application. How can I detect if my application was launched from its WidgetKit widget extension?
I'm unable to find any documentation on capturing this in the applications AppDelegate and/or SceneDelegate.
To detect an app launch from a WidgetKit widget extension where the parent application supports scenes you'll need to implement scene(_:openURLContexts:), for launching from a background state, and scene(_:willConnectTo:options:), for launching from a cold state, in your parent application's SceneDelegate. Also, add widgetURL(_:) to your widget's view.
Widget's View:
struct WidgetEntryView: View {
var entry: SimpleEntry
private static let deeplinkURL: URL = URL(string: "widget-deeplink://")!
var body: some View {
Text(entry.date, style: .time)
.widgetURL(WidgetEntryView.deeplinkURL)
}
}
Parent application's SceneDelegate:
// App launched
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _: UIWindowScene = scene as? UIWindowScene else { return }
maybeOpenedFromWidget(urlContexts: connectionOptions.urlContexts)
}
// App opened from background
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
maybeOpenedFromWidget(urlContexts: URLContexts)
}
private func maybeOpenedFromWidget(urlContexts: Set<UIOpenURLContext>) {
guard let _: UIOpenURLContext = urlContexts.first(where: { $0.url.scheme == "widget-deeplink" }) else { return }
print("🚀 Launched from widget")
}
If you are setting widgetURL or Link control for Widget UI then containing app opens with application(_:open:options:). You can set additional data in URL to know source.
If you are not using widgetUrl or link control then containing app opens with application(_:continue:restorationHandler:) and userInfo has WidgetCenter.UserInfoKey. That should tell you App opened from widget and information about user's interaction.
SwiftUI 2 life cycle
Add widgetURL to your Widget view:
struct SimpleWidgetEntryView: View {
var entry: SimpleProvider.Entry
private static let deeplinkURL = URL(string: "widget-deeplink://")!
var body: some View {
Text("Widget")
.widgetURL(Self.deeplinkURL)
}
}
Detect if the app is opened with a deeplink in onOpenURL:
#main
struct WidgetTestApp: App {
#State var linkActive = false
var body: some Scene {
WindowGroup {
NavigationView {
VStack {
NavigationLink("", destination: Text("Opened from Widget"), isActive: $linkActive).hidden()
Text("Opened from App")
}
}
.onOpenURL { url in
guard url.scheme == "widget-deeplink" else { return }
linkActive = true
}
}
}
}
Here is a GitHub repository with different Widget examples including the DeepLink Widget.
Related
In a SwiftUI app, I am facing a new challenge and hope someone can give me some hints or guidance. The mechanisms I have seen up to now to communicate between the parts of an app don't seem to quite fit here. But it is probably due to my still limited experience with SwiftUI.
First here is the relevant code:
class SceneDelegate {
... lot of code irrelevant to the question ...
func scene(_ scene: UIScene,
continue userActivity: NSUserActivity) {
... useful things happening for the app ...
// Now the view should change ... by some mechanism.
// This is the point of the question.
}
}
and:
struct ContentView: View {
... lot of code irrelevant to the question ...
var body: some View {
VStack {
... code to draw the view ...
}
... more code to draw the view ...
}
}
Second, my question is: how do I make my view to redraw itself, after processing has been performed inside scene(:continue ?
I had in mind some ideas, to do things in the scene(:continue function which would influence the drawing of the view.
Unfortunately, when trying to implement, I realized the code drawing the view was executed before the scene(:continue function. Therefore I need some other mechanism (notification like, bindings like, or ??) to have the view redrawn.
Is there a good practice or standard way of doing that?
It would be appropriate to use EnvironmentObject in this scenario
class AppState: ObservableObject
#Published var someVar: Sometype
}
class SceneDelegate {
let appState = AppState()
func scene(_ scene: UIScene,
continue userActivity: NSUserActivity) {
// ... other code
appState.someVar = ... // modify
}
}
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// .. other code
let contentView = ContentView()
.environmentObject(appState)
//
}
}
struct ContentView: View {
#EnvironmentObject var appState
var body: some View {
VStack {
// ... other code
appState.someVar // use here as needed
}
}
}
The goal is to have easy access to hosting window at any level of SwiftUI view hierarchy. The purpose might be different - close the window, resign first responder, replace root view or contentViewController. Integration with UIKit/AppKit also sometimes require path via window, so…
What I met here and tried before,
something like this
let keyWindow = shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
or via added in every SwiftUI view UIViewRepresentable/NSViewRepresentable to get the window using view.window looks ugly, heavy, and not usable.
Thus, how would I do that?
SwiftUI Lift-Cycle (SwiftUI 2+)
Here is a solution (tested with Xcode 13.4), to be brief only for iOS
We need application delegate to create scene configuration with our scene delegate class
#main
struct PlayOn_iOSApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
// ...
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
if connectingSceneSession.role == .windowApplication {
configuration.delegateClass = SceneDelegate.self
}
return configuration
}
}
Declare our SceneDelegate and confirm it to both (!!!+) UIWindowSceneDelegate and ObservableObject
class SceneDelegate: NSObject, ObservableObject, UIWindowSceneDelegate {
var window: UIWindow? // << contract of `UIWindowSceneDelegate`
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
self.window = windowScene.keyWindow // << store !!!
}
}
Now we can use our delegate anywhere (!!!) in view hierarchy as EnvironmentObject, because (bonus of confirming to ObservableObject) SwiftUI automatically injects it into ContentView
#EnvironmentObject var sceneDelegate: SceneDelegate
var body: some View {
// ...
.onAppear {
if let myWindow = sceneDelegate.window {
print(">> window: \(myWindow.description)")
}
}
}
Complete code in project is here
UIKit Life-Cycle
Here is the result of my experiments that looks appropriate for me, so one might find it helpful as well. Tested with Xcode 11.2 / iOS 13.2 / macOS 15.0
The idea is to use native SwiftUI Environment concept, because once injected environment value becomes available for entire view hierarchy automatically. So
Define Environment key. Note, it needs to remember to avoid reference cycling on kept window
struct HostingWindowKey: EnvironmentKey {
#if canImport(UIKit)
typealias WrappedValue = UIWindow
#elseif canImport(AppKit)
typealias WrappedValue = NSWindow
#else
#error("Unsupported platform")
#endif
typealias Value = () -> WrappedValue? // needed for weak link
static let defaultValue: Self.Value = { nil }
}
extension EnvironmentValues {
var hostingWindow: HostingWindowKey.Value {
get {
return self[HostingWindowKey.self]
}
set {
self[HostingWindowKey.self] = newValue
}
}
}
Inject hosting window in root ContentView in place of window creation (either in AppDelegate or in SceneDelegate, just once
// window created here
let contentView = ContentView()
.environment(\.hostingWindow, { [weak window] in
return window })
#if canImport(UIKit)
window.rootViewController = UIHostingController(rootView: contentView)
#elseif canImport(AppKit)
window.contentView = NSHostingView(rootView: contentView)
#else
#error("Unsupported platform")
#endif
use only where needed, just by declaring environment variable
struct ContentView: View {
#Environment(\.hostingWindow) var hostingWindow
var body: some View {
VStack {
Button("Action") {
// self.hostingWindow()?.close() // macOS
// self.hostingWindow()?.makeFirstResponder(nil) // macOS
// self.hostingWindow()?.resignFirstResponder() // iOS
// self.hostingWindow()?.rootViewController?.present(UIKitController(), animating: true)
}
}
}
}
Add the window as a property in an environment object. This can be an existing object that you use for other app-wide data.
final class AppData: ObservableObject {
let window: UIWindow? // Will be nil in SwiftUI previewers
init(window: UIWindow? = nil) {
self.window = window
}
}
Set the property when you create the environment object. Add the object to the view at the base of your view hierarchy, such as the root view.
let window = UIWindow(windowScene: windowScene) // Or however you initially get the window
let rootView = RootView().environmentObject(AppData(window: window))
Finally, use the window in your view.
struct MyView: View {
#EnvironmentObject private var appData: AppData
// Use appData.window in your view's body.
}
Access the current window by receiving NSWindow.didBecomeKeyNotification:
.onReceive(NotificationCenter.default.publisher(for: NSWindow.didBecomeKeyNotification)) { notification in
if let window = notification.object as? NSWindow {
// ...
}
}
At first I liked the answer given by #Asperi, but when trying it in my own environment I found it difficult to get working due to my need to know the root view at the time I create the window (hence I don't know the window at the time I create the root view). So I followed his example, but instead of an environment value I chose to use an environment object. This has much the same effect, but was easier for me to get working. The following is the code that I use. Note that I have created a generic class that creates an NSWindowController given a SwiftUI view. (Note that the userDefaultsManager is another object that I need in most of the windows in my application. But I think if you remove that line plus the appDelegate line you would end up with a solution that would work pretty much anywhere.)
class RootViewWindowController<RootView : View>: NSWindowController {
convenience init(_ title: String,
withView rootView: RootView,
andInitialSize initialSize: NSSize = NSSize(width: 400, height: 500))
{
let appDelegate: AppDelegate = NSApplication.shared.delegate as! AppDelegate
let windowWrapper = NSWindowWrapper()
let actualRootView = rootView
.frame(width: initialSize.width, height: initialSize.height)
.environmentObject(appDelegate.userDefaultsManager)
.environmentObject(windowWrapper)
let hostingController = NSHostingController(rootView: actualRootView)
let window = NSWindow(contentViewController: hostingController)
window.setContentSize(initialSize)
window.title = title
windowWrapper.rootWindow = window
self.init(window: window)
}
}
final class NSWindowWrapper: ObservableObject {
#Published var rootWindow: NSWindow? = nil
}
Then in my view where I need it (in order to close the window at the appropriate time), my struct begins as the following:
struct SubscribeToProFeaturesView: View {
#State var showingEnlargedImage = false
#EnvironmentObject var rootWindowWrapper: NSWindowWrapper
var body: some View {
VStack {
Text("Professional Version Upgrade")
.font(.headline)
VStack(alignment: .leading) {
And in the button where I need to close the window I have
self.rootWindowWrapper.rootWindow?.close()
It's not quite as clean as I would like it to be (I would prefer to have a solution where I did just say self.rootWindow?.close() instead of requiring the wrapper class), but it isn't bad and it allows me to create the rootView object before I create the window.
Instead of ProjectName_App use old fashioned AppDelegate approach as app entry point.
#main
final class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
...
}
}
Then pass window as environment object. For example:
struct WindowKey: EnvironmentKey {
static let defaultValue: UIWindow? = nil
}
extension EnvironmentValues {
var window: WindowKey.Value {
get { return self[WindowKey.self] }
set { self[WindowKey.self] = newValue }
}
}
final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let rootView = RootView()
.environment(\.window, window)
window?.rootViewController = UIHostingController(rootView: rootView)
window?.makeKeyAndVisible()
}
}
And use it when need it.
struct ListCell: View {
#Environment(\.window) private var window
var body: some View {
Rectangle()
.onTapGesture(perform: share)
}
private func share() {
let vc = UIActivityViewController(activityItems: [], applicationActivities: nil)
window?.rootViewController?.present(vc, animated: true)
}
}
2022, macOS only
Maybe not best solution, but works well for me and enough universal for almost any situation
Usage:
someView()
.wndAccessor {
$0?.title = String(localized: "All you need to know about FileBo in ONE minute")
}
extension code:
import SwiftUI
#available(OSX 11.0, *)
public extension View {
func wndAccessor(_ act: #escaping (NSWindow?) -> () )
-> some View {
self.modifier(WndTitleConfigurer(act: act))
}
}
#available(OSX 11.0, *)
struct WndTitleConfigurer: ViewModifier {
let act: (NSWindow?) -> ()
#State var window: NSWindow? = nil
func body(content: Content) -> some View {
content
.getWindow($window)
.onChange(of: window, perform: act )
}
}
//////////////////////////////
///HELPERS
/////////////////////////////
// Don't use this:
// Usage:
//.getWindow($window)
//.onChange(of: window) { _ in
// if let wnd = window {
// wnd.level = .floating
// }
//}
#available(OSX 11.0, *)
private extension View {
func getWindow(_ wnd: Binding<NSWindow?>) -> some View {
self.background(WindowAccessor(window: wnd))
}
}
#available(OSX 11.0, *)
private struct WindowAccessor: NSViewRepresentable {
#Binding var window: NSWindow?
public func makeNSView(context: Context) -> NSView {
let view = NSView()
DispatchQueue.main.async {
self.window = view.window
}
return view
}
public func updateNSView(_ nsView: NSView, context: Context) {}
}
I am porting an iPad app using Mac Catalyst. I am trying to open a View Controller in a new window.
If I were using strictly AppKit I could do something as described in this post. However, since I am using UIKit, there is no showWindow() method available.
This article states that this is possible by adding AppKit in a new bundle in the project (which I did), however it doesn't explain the specifics on how to actually present the new window. It reads...
Another thing you cannot quite do is spawn a new NSWindow with a UIKit view hierarchy. However, your UIKit code has the ability to spawn a new window scene, and your AppKit code has the ability to take the resulting NSWindow it's presented in and hijack it to do whatever you want with it, so in that sense you could spawn UIKit windows for auxiliary palettes and all kinds of other features.
Anyone know how to implement what is explained in this article?
TL;DR: How do I open a UIViewController as a new separate NSWindow with Mac Catalyst?
EDIT : ADDED INFO ON HOW TO HAVE ADDITIONAL DIFFERENT WINDOWS LIKE PANELS
In order to support multiple windows on the mac, all you need to do is to follow supporting multiple windows on the iPad.
You can find all the information you need in this WWDC session starting minute 22:28, but to sum it up what you need to do is to support the new Scene lifecycle model.
Start by editing your target and checking the support multiple window checkmark
Once you do that, click the configure option which should take you to your info.plist.
Make sure you have the proper entry for Application Scene Manifest
Create a new swift file called SceneDelegate.swift and just paste into it the following boilerplate code
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene) {
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}
And you're basically done. Run your app, and hit command + N to create as many new windows as you want.
If you want to create a new window in code you can use this:
#IBAction func newWindow(_ sender: Any) {
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: nil, options: nil) { (error) in
//
}
}
And now we get to the big mystery of how to create additional windows
The key to this is to create multiple scene types in the app. You can do it in info.plist which I couldn't get to work properly or in the AppDelegate.
Lets change the function to create a new window to:
#IBAction func newWindow(_ sender: Any) {
var activity = NSUserActivity(activityType: "panel")
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: nil) { (error) in
}
}
Create a new storyboard for your new scene, create at least one viewcontroller and make sure to set in as the initalviewcontroller in the storyboard.
Lets add to the appdelegate the following function:
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
if options.userActivities.first?.activityType == "panel" {
let configuration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
configuration.delegateClass = CustomSceneDelegate.self
configuration.storyboard = UIStoryboard(name: "CustomScene", bundle: Bundle.main)
return configuration
} else {
let configuration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
configuration.delegateClass = SceneDelegate.self
configuration.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
return configuration
}
}
By setting the userActivity when requesting a scene we can know which scene to create and create the configuration for it accordingly. New Window from the menu or CMD+N will still create your default new window, but the new window button will now create the UI from your new storyboard.
And tada:
With SwiftUI you can do it like this (thanks to Ron Sebro):
1. Activate multiple window support:
2. Request a new Scene:
struct ContentView: View {
var body: some View {
VStack {
// Open window type 1
Button(action: {
UIApplication.shared.requestSceneSessionActivation(nil,
userActivity: NSUserActivity(activityType: "window1"),
options: nil,
errorHandler: nil)
}) {
Text("Open new window - Type 1")
}
// Open window type 2
Button(action: {
UIApplication.shared.requestSceneSessionActivation(nil,
userActivity: NSUserActivity(activityType: "window2"),
options: nil,
errorHandler: nil)
}) {
Text("Open new window - Type 2")
}
}
}
}
3. Create your new window views:
struct Window1: View {
var body: some View {
Text("Window1")
}
}
struct Window2: View {
var body: some View {
Text("Window2")
}
}
4. Change SceneDelegate.swift:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
if connectionOptions.userActivities.first?.activityType == "window1" {
window.rootViewController = UIHostingController(rootView: Window1())
} else if connectionOptions.userActivities.first?.activityType == "window2" {
window.rootViewController = UIHostingController(rootView: Window2())
} else {
window.rootViewController = UIHostingController(rootView: ContentView())
}
self.window = window
window.makeKeyAndVisible()
}
}
I'm doing a sign-in screen. after the sign-in button click, I'll do an API call on the response I will redirect the user to home screen.
Q - How to navigate from one View to Another by a button click with some action.
I've tried this,
Programatically navigate to new view in SwiftUI
But I'm getting an error like,
"Function declares an opaque return type, but has no return statements in its body from which to infer an underlying type"
Please help me to resolve it.
There is an issue with some View ATM (hope it will be fixed soon).
In order to use code posted, you will need to write something like:
struct ContentView: View {
#EnvironmentObject var userAuth: UserAuth
#ViewBuilder
var body: some View {
if !userAuth.isLoggedin {
return LoginView()
} else {
return NextView()
}
}
}
At the list, at the moment of writing, this was the only thing working for me - both for body and for Group.
Reference for future: date 24 Oct 2019.
Alternatively look at SceneDelgate.swift where you can set the root view of the key window to whatever you want.
In your situation when there is a successful login you can signal the change of state to the SceneDelegate (such as by Notification). Then have the app set the root view controller to your main View (as a UIHostingController).
For example:
In your SceneDelegate class add:
var currentScene: UIScene? // need to keep reference
Then inside the func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
Save a reference to the scene in the variable declared above.
self.currentScene = scene
Next, add a listener to when you want to change the key window with the new view:
var keyWindow: UIWindow?
NotificationCenter.default.addObserver(forName: .newUser, object: nil, queue: .main) { [weak self] (notification) in
guard let windowScene = self?.currentScene as? UIWindowScene else { return }
keyWindow = UIWindow(windowScene: windowScene)
keyWindow?.rootViewController = UIHostingController(rootView: Text("hello"))
keyWindow?.makeKeyAndVisible()
}
Just set the Notification to post whenever you need and replace the Text with whatever View you want.
How can an app access the XIB or storyboard used for its launch screen? The XIB is not in the main bundle (ex: NSBundle.mainBundle().pathsForResourcesOfType(nil, inDirectory: "")). This is especially unexpected since "Launch Screen.xib" is listed in the "Copy Bundle Resources" build phase but doesn't show ip in the bundle, so Xcode must be treating it specially.
If LaunchScreen is storyboard and not a xib, Use the following code.
let launchScreen = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()
if let launchView = launchScreen?.view {
view.addSubview(launchView)
}
As Xib is not in the main bundle getting path returns nil, But you can get the XIB of the launch screen without the help of path using method
let launchScreenNib = UINib(nibName: "LaunchScreen", bundle: nil)
or
You can load get views from XIB as
// Swift
let objects = NSBundle.mainBundle().loadNibNamed("LaunchScreen", owner: self, options: nil)
let view = objects[0] as UIView
// Obj C
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:#"LaunchScreen" owner:self options:nil];
UIView *view = [objects objectAtIndex:0];
Following up to my own question, it looks like toggling "Use as Launch Screen" off and on caused Xcode to build a NIB file for the launch screen.
After doing a complete clean (removed DerivedData) and deleting the app from the device, I toggled off "Use as Launch Screen" for the main view controller in LaunchScreen.xib. Running the app caused it to launch without a launch screen, but the build was now creating LaunchScreen.nib.
Did a complete clean again and deleted the app from the device. Toggled "Use as Launch Screen" back on and rebuilt. In the app bundle under the new DerivedData folder, LaunchScreen.nib was still there.
bundle.loadNibNamed(...) worked fine now.
Here's a Swift 5, SceneDelegate implementation
This implementation also detects the LaunchScreen's configured name from the info.plist file.
//
// SceneDelegate.swift
// Extended Launch Screen Example
//
// Created by Leslie Godwin on 2020/04/24.
//
import UIKit
class SceneDelegate: UIResponder, UIWindowSceneDelegate
{
var window: UIWindow?
var splashWindow: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)
{
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let _ = (scene as? UIWindowScene) else { return }
}
func sceneDidDisconnect(_ scene: UIScene)
{
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene)
{
// ⏳Extend Splash screen only if `splashWindow` is `nil` otherwise it is already shown.
//
if let launchStoryboardName = InfoPList.launchStoryboardName,
let windowScene = self.window?.windowScene
{
splashWindow = splashWindow ??
{
let window = UIWindow(windowScene: windowScene)
window.windowLevel = .statusBar
let storyboard = UIStoryboard(name: launchStoryboardName, bundle: nil)
window.rootViewController = storyboard.instantiateInitialViewController()
window.isHidden = false
// ⏳Wait for 5 seconds, then remove.
//
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(5))
{
UIView.animate(withDuration: -1, // system default
animations:
{
self.splashWindow?.alpha = 0
},
completion:
{ _ in
self.splashWindow?.isHidden = true
self.splashWindow = nil
}
)
}
return window
}()
}
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
func sceneWillEnterForeground(_ scene: UIScene) {
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
func sceneDidEnterBackground(_ scene: UIScene) {
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}
}
//
// PList Helper
//
struct InfoPList
{
private static func value(for name: String) -> String? { Bundle.main.object(forInfoDictionaryKey: name) as? String }
static var bundleIdentifier: String? { self.value(for: "CFBundleIdentifier") }
static var bundleDisplayName: String? { self.value(for: "CFBundleDisplayName") }
static var bundleShortVersionString: String? { self.value(for: "CFBundleShortVersionString") }
static var bundleVersion: String? { self.value(for: "CFBundleVersion") }
static var launchStoryboardName: String? { self.value(for: "UILaunchStoryboardName") }
static var mainStoryboardName: String? { self.value(for: "UIMainStoryboardFile") }
}