0

Edit

I mistakenly selected "Multiplatform" instead of "iOS" when creating the project.
Simply select "iOS" to create the project and things will be done the traditional way.


I want to start a new project in Xcode 14.2 with just AppDelegate and UIKit, without SwiftUI or SceneDelegate. The method up to Xcode 13 does not seem to work.

1. Create new project in Xcode 14.2
2. Add AppDelegate.swift
import UIKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        window = UIWindow(frame: UIScreen.main.bounds)
        window!.rootViewController = MyViewController()
        window!.makeKeyAndVisible()
        return true
    }
}
3. Add MyViewController.swift
import UIKit

class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .systemGreen

        print("viewDidLoad() called.")
        print("frame:", view!.frame)
    }
}

When the application is launched, a green screen is expected to appear, but a black screen is displayed.
Print statements are output correctly.

At this time, the following warning appears.
Adding UIApplicationSceneManifest key to Info.plist will remove this warning, but the black screen will remain. (Previously, this key was not needed for apps that did not use multiple windows.)

[SceneConfiguration] Info.plist contained no UIScene configuration dictionary (looking for configuration named "(no name)")

What simple something am I missing?

Mac
  • 137
  • 1
  • 8
  • 1
    Does this answer your question? [How to remove Scene Delegate from iOS Application?](https://stackoverflow.com/questions/59006550/how-to-remove-scene-delegate-from-ios-application) or [Opt out of UISceneDelegate/SwiftUI on iOS](https://stackoverflow.com/questions/57467003/opt-out-of-uiscenedelegate-swiftui-on-ios) – HangarRash Jan 12 '23 at 02:50

1 Answers1

2

I assume you want to invoke your app by initializing a UIViewController and don't care much about the underlying UIResponder objects.

Create a new project by selecting Storyboard as the desired Interface preference (You won't use a storyboard after all). enter image description here

Then add your first View Controller to the window object. And if you'd like to access your AppDelegate you can do it from anywhere as below.

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        
        guard let window = window else { return }
        
        let vc = UIViewController()
        vc.view.backgroundColor = .systemGreen
        window.rootViewController = vc
        window.makeKeyAndVisible()
        
        let appDelegate = UIApplication.shared.delegate as? AppDelegate
    }
}

Correct me if your intention of omitting UIWindowSceneDelegate is of other nature, such as supporting iOS 12

Elio_
  • 398
  • 4
  • 13
  • I had selected "Multiplatform" instead of "iOS" when creating the project. Your answer helped me to realize this mistake. Thanks. – Mac Jan 12 '23 at 09:46