I have an app made in Xcode 10 using Main.storyboard and would like to migrate it to use Apple's new framework: SwiftUI.
Is that already possible?
I have already tried to add the UIApplicationSceneManifest key in Info.plist, I changed the AppDelegate.swift to use scenes, I created the SceneDelegate.swift and even then I could not
I assume you're using Xcode 11 GM and macOS Mojave or Catalina.
Along with the changes in the plist, you have to add UISceneSession lifecycle functions in the application delegate.
func application(_ application: UIApplication,
configurationForConnecting connectingSceneSession: UISceneSession,
options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// The name must match the one in the Info.plist
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
Also, you need to make sure the window is created correctly in the SceneDelegate.
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else {
return
}
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()
}
where ContentView is the main SwiftUI view you want to display.
P.S. Make sure the plist specifies $(PRODUCT_MODULE_NAME).SceneDelegate as delegate class name, and the scene delegate is called SceneDelegate
Example:
If you're on Catalina, you can turn on Previews in the build settings for your target.
Build Options -> Enable Previews
Addendum I:
Make sure you remove the Storyboard key from the Info.Plist and that you're targeting iOS 13.
Addendum II:
Clean Derived Data, as many devs in the comments suggest.
It's a correct only minor change
In SceneDelegate.swift replace
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()
with
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: ContentView())
self.window = window
window.makeKeyAndVisible()
}
TAKEN FROM HERE
My solution turned out to be different. In my case, I had everything in place, but when the code attempted to load the UISceneConfiguration, it failed to load the config in the Info.plist and gave me a secondary window config instead with no scene delegate set. If I asked for the correct configuration from the debug console it would load as expected. I was confused.
I double checked everything and tried all of the suggestions here but none worked. In the end I did 'Hardware' - 'Erase all contents and settings...' on the simulator and that solved it.
My guess is that because I'd been running the pre-SwiftUI version of the app on the simulator, something in that caused the SwiftUI version to behave differently.
Related
I am creating an App which features an Onboarding. This Onboarding takes place on a ViewController with only this specific purpose, that's why I only want the ViewController to show up ones after downloading the app.(the main app consists out of only one ViewController)
Here is what I've done so far:
1) I delete the storyboard entry point in the storyboard file
2) entered the Storyboard ID for each of the two ViewController
3) coded in the AppDelegate file that the onboarding ViewContoller only should show up the first time after the download
My problem: When running the app on the simulator it only shows a black screen
I already made sure that the Indentifiers and the name of the storyboard are correct.
(below my AppDelegate.swift file)
import UIKit
import Firebase
import FirebaseDatabase
import paper_onboarding
#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.
FirebaseApp.configure()
window = UIWindow(frame: UIScreen.main.bounds)
let sb = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = sb.instantiateViewController(withIdentifier: "Onboarding")
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "onboardingComplete") {
initialViewController = sb.instantiateViewController(withIdentifier: "MainApp")
}
window?.rootViewController = initialViewController
window?.makeKeyAndVisible()
return true
}
// 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.
}
}
``
For black screen:
Have you checked MAIN INERFACE in General tab of project's target? It should be set to one of the storyboard choosing from the dropdown. Also check if Launch Screen File have the LaunchScreen or any other required storyboard selected.
For Onboarding as the first view:
Main interface should be given to the storyboard where the onboarding view controller is and then assigning that view as the initial view controller.
Let me know if you don't understand the logic.
The solution for this problem is not to use the AppDelegate.swift file but the SceneDelegate.swift file.
below my SceneDelegate.swift file
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).
let sb = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = sb.instantiateViewController(withIdentifier: "Onboarding")
let userDefaults = UserDefaults.standard
if userDefaults.bool(forKey: "onboardingComplete") {
initialViewController = sb.instantiateViewController(withIdentifier: "MainApp")
}
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = initialViewController
self.window = window
window.makeKeyAndVisible()
}
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.
}
}
I have been trying to learn the Coordinator pattern by creating a new app on Xcode 11.2 using Storyboards as Interface design.
I followed this video by Paul Hudson but I got stuck at minute 12 when code needed to be added to the AppDelegate.swift file. Like it is the app will launch, the first view controller will show but it will not navigate.
What should I change or, better, where should I move the present code to make it work?
Whole project can be found here.
In short the code that in iOS 12 and before was in AppDelegate is this:
var coordinator: MainCoordinator?
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let navController = UINavigationController()
coordinator = MainCoordinator(navigationController: navController)
coordinator?.start()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
I have seen that now window is in SceneDelegate but moving everything there to the sceneDidConnect method is not helping.
Can someone enlighten me here?
Thanks!
So a few changes have to be made to be able to implement this pattern. Firstly, you should restore your AppDelegate to it's initial format on creation:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
You can remove the var coordinator: MainCoordinator? declaration at the top.
In SceneDelegate replace the code in the sceneWillConnectToSession function with the following:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
let navController = UINavigationController()
let coordinator = MainCoordinator(navigationController: navController)
coordinator.start()
let window = UIWindow(windowScene: windowScene)
window.rootViewController = navController
self.window = window
window.makeKeyAndVisible()
}
The final change is that I removed the weak declaration for MainCoordinator in your view controllers.
So I just replaced it with var coordinator: MainCoordinator?, and then it worked.
Reference Article: The Scene Delegate In Xcode 11 And iOS 13
If I compile onto an iOS 12 device (doesn't use UIScene) and AirPlay Mirror to my Apple TV the app is mirrored as expected to the TV.
On an iOS 13 device, it seems to treat it as an external display where it's formatted to fit the screen (but I have no way to control it).
I'd prefer the old functionality of just mirroring it.
How do I accomplish mirroring on iOS 13? I'm digging around in the docs for:
application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration
And in the UISceneConfiguration there's a role property (it has UISceneSession.Role.windowExternalDisplay when I try to AirPlay Mirror) but it doesn't seem to have any value like UISceneSession.Role.windowMirror.
I've been playing around with mirroring and external displays and various possibilities exist with just the right combination of code/settings but certain functionality doesn't seem possible.
Under iOS 13 (with an app built with a Base SDK of iOS 13), you can get your app to be mirrored on an external display. But making this work prevents your app from showing different content on an external display. Basically your app only mirrors or it only shows a unique scene for an external display.
If you wish to only have your app be mirrored, then ensure the following:
Remove the application(_:configurationForConnecting:options:) from your App Delegate.
In the Info.plist, make sure there is no entry for the "External Display Session Role" under the "Scene Configuration" section of the "Application Scene Manifest".
If neither of those two things are part of your app then your app will simple mirror to any external screen when you activate Screen Mirroring on the iOS device.
Just ran into this issue myself. My solution actually came from within my UIWindowSceneDelegate class.
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = (scene as? UIWindowScene) else { return }
// External displays should not get assigned a window. When a window isn't assigned, the default behavior is mirroring.
guard session.role != .windowExternalDisplay else { return }
/* the rest of your setup */
}
When you don't assign a window, it seems that mirroring becomes the default option. Before that change, my external displays (screen mirroring) were given their own unique UIWindow instance.
I don't see this documented anywhere, and it is not intuitive. Because of this, I'm somewhat fearful that it will break in the future.
Hope it still helps.
I found that with Objective-C implementation, you can achieve the screen mirroring behavior by returning nil in application:configurationForConnectingSceneSession:options:.
- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
if (connectingSceneSession.role == UIWindowSceneSessionRoleExternalDisplay) {
return nil;
}
UISceneConfiguration *configuration = [[UISceneConfiguration alloc] initWithName:#"Main" sessionRole:connectingSceneSession.role];
configuration.storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
configuration.delegateClass = [SceneDelegate class];
configuration.sceneClass = [UIWindowScene class];
return configuration;
}
Be aware that this is not a documented way and may break in the future.
Edited:
In Swift, you can achieve this via method swizzling:
#UIApplicationMain
class AppDelegate : UIResponder, UIApplicationDelegate {
override init() {
_ = AppDelegate.performSceneConfigurationSwizzle
super.init()
}
private static let performSceneConfigurationSwizzle: Void = {
method_exchangeImplementations(
class_getInstanceMethod(AppDelegate.self, #selector(AppDelegate.application(_:configurationForConnecting:options:)))!,
class_getInstanceMethod(AppDelegate.self, #selector(AppDelegate.swizzle_application(_:configurationForConnecting:options:)))!
)
}()
#objc func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
fatalError("Should never reach.")
}
#objc private func swizzle_application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration? {
if connectingSceneSession.role == .windowExternalDisplay {
return nil
}
// build scene configuration as usualā¦
}
}
Instead of implementing the AppDelegate scene configuration method in iOS 13:
#available(iOS 13.0, *)
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
let configuration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
configuration.delegateClass = SceneDelegate.self
return configuration
}
I instead switched to using the Info.plist variant (and removed the above code) where you effectively specify all the above in your Info.plist instead. (For an up to date version of what's expected in the Info.plist file, simply create a New Project in Xcode and copy the contents from the new Info.plist file for the Application Scene Manifest key).
It now works perfectly and AirPlay Mirror mirrors as expected. I did try changing the role to windowApplication as iOS seemingly does with the Info.plist variant but it still doesn't work.
My apps uses no Storyboards. All views are created programmatically.
Historically I have deleted my Main.storyboard, removed the reference from my Info.plist and setup my UIWindow and rootViewController as follows:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIViewController()
window.makeKeyAndVisible()
self.window = window
return true
}
However when trying to run my app in iOS 13, I get a crash -
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Could not find a storyboard named 'Main' in bundle NSBundle </Users/Dev/Library/Developer/CoreSimulator/Devices/8A4474B1-FCA3-4720-8F34-A6975A03EE19/data/Containers/Bundle/Application/258FA246-A283-4FE6-A075-58BD32424427/Home.app> (loaded)'
***
iOS 12 still runs as expected. How should I setup my view programmatically to support iOS 12 and 13?
You need to add update SceneDelegate.swift also.
Update func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) and add
guard let windowScene = (scene as? UIWindowScene) else { return }
let window = UIWindow(windowScene: windowScene)
let viewController = ViewController()
window.rootViewController = viewController
window.makeKeyAndVisible()
self.window = window
This question already has answers here:
Why is manually setup root view controller showing black screen?
(3 answers)
Closed 3 years ago.
I want to initialize window inside appDelegate to show specific ViewController depend on some cases. Now I have this code:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow()
let rootNavigationController = UIViewController()
window?.rootViewController = rootNavigationController
window?.rootViewController?.view.backgroundColor = .green
window?.makeKeyAndVisible()
return true
}
// 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.
}
}
I'm using XCode11 and have created new project. SceneDelegateĀ file I removed cause it hasn't effect on this. Also removed Main from Info.plist and from deployment info
As result on device I see black screen but debugger show me that rootNavigationController as should be
image from debugger
How fix it or implement this logic for XCode11?
Solution:
1)Inside manifest(plist) file remove Storyboard Name field
2)inside SceneDelegate.swift implement:
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 windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let rootNavigationController = UIViewController()
window?.rootViewController = rootNavigationController
window?.rootViewController?.view.backgroundColor = .green
window?.makeKeyAndVisible()
}
And it will work for ios 13 , if you want support ios 12 and lower you need also implement this logic in AppDelegate