2

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?

Tim J
  • 1,211
  • 1
  • 14
  • 31
  • Please check in your Target->General Tab ->Deployment Info->Main Interface. it may be Main.Storyboard – Samir Shaikh Oct 09 '19 at 04:23
  • See https://stackoverflow.com/a/58208876/1226963 for the general pattern you need. Even easier, see https://github.com/rmaddy/XcodeTemplates for a template that support iOS 12 & 13, both Swift and Objective-C, and either code-only or storyboards. – rmaddy Oct 09 '19 at 05:25

1 Answers1

1

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
nodediggity
  • 2,458
  • 1
  • 9
  • 12
  • This is not quite right. You must use the `UIWindow` initializer that takes a window scene. And as a result you do not need the `window.windowScene = windowScene` line. – rmaddy Oct 09 '19 at 05:18
  • Excellent spot, I've updated my answer. Thanks for pointing that out – nodediggity Oct 09 '19 at 05:35