How get rootViewController with iPadOS multi window (SceneDelegate)? - swift

I am using Xcode 11 (beta3) and building an app for iOS 13. In my project I created the delegate methods for UIWindowSceneDelegate declaring it in Info.plist.
Now I'm able to create multiple windows (and UIScene).
How can I access the rootViewController now I've not anymore a single window? I need it to get some reference to objects and bounds it holds.
In my AppDelegate window is nil, and in my ViewController (child view controller) instance I tried using self.view.window.rootViewController but I found out that viewDidLoad() is too soon (I think) and the window is still nil, works in viewDidAppear(), but I don't need to make this process every time the view controller appears.
What's the best practice with this new way to handle application scenes?
Here is my AppDelegate:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
return true
}
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
My SceneDelegate:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// yes it's empty, I'm using storyboard
}

Now you have more than one rootViewController, one for each scene.
First, you have to answer which one you need at the moment of usage.
Probably you want to get one of the rootViewController of the currently active scene then you can use this:
var rootVC:UIViewController? = nil
if #available(iOS 13.0, *) {
for scene in UIApplication.shared.connectedScenes {
if scene.activationState == .foregroundActive {
rootVC = ((scene as? UIWindowScene)!.delegate as! UIWindowSceneDelegate).window!!.rootViewController
break
}
}
} else {
// Fallback on earlier versions
}

You can access connected scenes using:
UIApplication.shared.connectedScenes
As per Apple documentation:
Connected scenes are those that are in memory and potentially doing active work. A connected scene may be in the foreground or background, and it may be onscreen or offscreen.
Then you can iterate through them and try to get UIWindowScene from there.
guard let windowScene = (scene as? UIWindowScene) else { return }
print(windowScene.windows) // This will print windows associated with the scene.
On the other hand, from the view controller you can access the window through the view:
self.view.window

Related

Change a scene on app becoming active in Swift

I am working to ensure that the user doesn't change their time and currently I test it when the app is starting and if their time is incorrect I perform a segue to a new screen and create an alert to make them change it. However if the user changes their time while the app is open the check won't run. I think it is something with the SceneDelegate as that will run code when the app becomes active but I don't know how to transition it as the code that checks the time and performs the segue is located in ViewController. Using UI kit and webkit.
These are the functions responsable for calling the time error message. Both functions are located in ViewController file.
the BadTime class is defined outside of any other classes
class BadTime:UIViewController{
func Alert (Message: String){
let alert = UIAlertController(title: "Mobile 3.0 Alert", message: Message, preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: {(alert:UIAlertAction!) in exit(0)}))
self.present(alert, animated: true, completion: nil)
}
override func viewDidAppear(_ animated: Bool) {
self.Alert(Message: "Please change your time to be set automatically\nSettings->General->Date & Time->Set Automatically")
}
}
The segue is in the viewDidAppear function of the ViewController class.
self.performSegue(withIdentifier: "bad_time", sender: self)
The SceneDelegate file in which the sceneWillEnterForeground is the function that updates when ever the app becomes active
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).
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 necessarily discarded (see `application:didDiscardSceneSessions` instead).
}
func sceneDidBecomeActive(_ scene: UIScene) {
// viewControl.viewDidAppear(false)
}
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) {
print(scene)
// 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.
}
}

Cannot add iOS support for versions previous to 13 in Xcode 11

I've already followed this tutorial in both ways, by implementing the #available directives and by removing code/files, but still a black screen displays. Unless I'm missing something I think I've done both things properly.
This is the current status in the way of removing files/code:
SceneDelegate.swift deleted.
AppDelegate.swift like this:
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
}
And finally Info.plist like this:
What am I missing so the screen still displays black due to the program not supporting versions previous to iOS 13?
Add this code in your AppDelegate file in didFinishLaunchingWithOptions method
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 13.0, *) {
} else {
let rootVC = //Initialize root controller
self.window?.rootViewController = rootVC
self.window?.makeKeyAndVisible()
}
return true
}
I guide you step by step.
1st remove SceneDelegate file from the project.
Add var window: UIWindow? to AppDelegate.
Remove below func from AppDelegate.
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
Remove Application Scene Manifest key from Info.plist file.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let navigation = UINavigationController(rootViewController: ViewController())
let frame = UIScreen.main.bounds
window = UIWindow(frame: frame)
window!.rootViewController = navigation window!.makeKeyAndVisible()
return true
}
Extra
Add below delegate methods in AppDelegate file, below didFinishLaunchingWithOptions
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.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

How do I rename the initial programmatic ViewController class for my iOS app without involving storyboards

I have been looking for a while and cannot find an answer to this question that does not involve storyboards.
It's quite simple. When you build a new iOS app in Xcode you are given the ViewController.swift file that contains the initial ViewController() class for the app. This view controller is the main view for the app.
If I rename the class to say ViewControllerTest(), when I build and run the app now only loads a blank screen since it cannot find the ViewController class as I renamed it.
How do I set the project in Xcode to use the new ViewControllerTest() class as the initial view controller?
Thanks
If you want to disregard using the storyboard entirely, which I prefer because then all code is in one place, you need to add the following in your AppDelegate or SceneDelegate depending on what version Xcode you are creating your project with.
Xcode 10 and earlier
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
// Or whatever you want to name your view controller class
window.rootViewController = ViewController()
window.makeKeyAndVisible()
self.window = window // retain instance
return true
}
}
for Xcode 10 and earlier you should also change the value of the UIMainStoryboardFile property to an empty string in your Info.plist.
Xcode 11 and later
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 }
let window = UIWindow(windowScene: windowScene)
// Or whatever you want to name your view controller class
window.rootViewController = ViewController()
window.makeKeyAndVisible()
self.window = window // retain instance
}
}
for Xcode 11 projects and newer, you should populate the Application Scene Manifest property as shown here:

iOS 13/Facebook SDK error "Unable to find a valid UIWindow"

(To forestall well-intended suggestions, yes I posted this question last week on Facebook's developer forum. No responses yet.)
TL;DR
Facebook SDK 5.8 complains at startup FBSDKLog: Unable to find a valid UIWindow.
The Main Story
In a from-scratch, one-view Xcode 11/iOS 13 project, there is no longer a default UIWindow member associated with the application. (The window per se is still around; you can see it, contained in a UIWindowScene, using the View Hierarchy Debugger in Xcode, or the Reveal app.)
FBSDK 5.8 does seem to be iOS-13-aware, and looks around for it. The relevant code is at line 498 of
https://github.com/facebook/facebook-ios-sdk/blob/master/FBSDKCoreKit/FBSDKCoreKit/Internal/FBSDKInternalUtility.m.
Facebook's code iterates over the application's connectedScenes member, which for me is an empty set. How do I modify my code so that FBSDK finds the window?
Some Hacking
I tried adding the following to scene(_:willConnectTo:options:) but it seems to be too late — the FBSDKLog message has already appeared by then. (So I'm flailing...)
guard let s = (scene as? UIWindowScene) else { return }
self.window = UIWindow(windowScene: s)
The following also failed, but it was just a shot in the dark:
guard let s = (scene as? UIWindowScene) else { return }
self.window = UIWindow(frame: s.coordinateSpace.bounds)
self.window?.windowScene = s
self.window?.rootViewController = ViewController(nibName: nil, bundle: nil)
self.window?.makeKeyAndVisible()
If you don't use the new behaviour and don't mind reverting to the old way, you can try the following
Delete Application Scene Manifest key from Info.plist
Delete SceneDelegate.swift
Add var window: UIWindow?to AppDelegate.swift
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow? // <-- Here
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// window!.makeKeyAndVisible()
return true
}
}
Add window variable to AppDelegate class
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
Assign UIWindow inside didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
if let window = window {
window.makeKeyAndVisible()
self.window = window
}
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}

How to create UI programmatically in Xcode 11? [duplicate]

This question already has an answer here:
Xcode 11 & iOS13, using UIKIT can't change background colour of UIViewController
(1 answer)
Closed 3 years ago.
Update: Finally got it working. See below code for SceneDelegate.swift
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(frame: windowScene.coordinateSpace.bounds)
window?.windowScene = windowScene
window?.rootViewController = ViewController()
window?.makeKeyAndVisible()
}
In contrast to the tutorials and the articles online, I was not able to create a working UI with the latest Xcode. This was also the case with Xcode 10, but I did not care at the time.
My steps are as follows:
Delete Main.storyboard
Delete Main from project settings
Write basic UIWindow code:
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
let mainVC = ViewController()
window?.rootViewController = mainVC
return true
}
At this point I am getting an error saying cannot find the storyboard named Main in the bundle. If I go ahead and clear the entry from Info.plist, this time it complains that there are not enough characters in the storyboard name.
Any ideas?
These are the steps I followed to get UI working programmatically in Xcode 11.
1) Created a new project as usual
2) I deleted the Main.Storyboard file from the project navigator on the left-hand side.
3) In General tab I removed the Main option from the Main Interface drop-down list
4) Next, go to your Info.plist file and completely remove the Storyboard Name from the list
5) Now if you run the app, it should not crash and it should show you a black screen.
6) Next, go to the SceneDelegate.swift file and initialise your UIWindow object as usual. There is a catch here and is new in Xcode 11. You also set the windowScene property of the window object, or else it won't work.
7) Once that's all done, Go to your ViewController class and set the background colour of the view to some colour and you should be all good.
You can also use templates:
Xcode 11 Programmatic UIKit templates
Hope this helped you!!!
This works for me on Xcode 10.3 Swift 5, and Xcode 11 Swift 5.1.
On your new Xcode project, on the Info.plist file, delete the launch screen and main interface file name entries, (don't leave the entry there with an empty string)
Remove the #UIApplicationMain attribute from your AppDelegate class.
Also, this link may help you, you can find info on UIApplicationMain
https://docs.swift.org/swift-book/ReferenceManual/Attributes.html
See code below:
// Created by Juan Miguel Pallares Numa on 9/16/19.
// Copyright © 2019 Juan Miguel Pallares Numa. All rights reserved.
import UIKit
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var myViewController = ViewController()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = myViewController
window?.makeKeyAndVisible()
return true
}
}
import UIKit
class ViewController: UIViewController {
convenience init() {
self.init(nibName: nil, bundle: .main)
view = UIView(frame: UIScreen.main.bounds)
view.backgroundColor = UIColor(
displayP3Red: 0.0, green: 0.7, blue: 0.0, alpha: 1.0)
}
}
// call this file "main.swift"
import Foundation
import UIKit
UIApplicationMain(
CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(AppDelegate.self))