Forked from pavankataria/scene-delegates-programmatic-window-creation.swift
Created
December 28, 2023 06:05
-
-
Save munho/97a16ce3585b798e46f79ffd7e94401d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { | |
/// 1. Capture the scene | |
guard let windowScene = (scene as? UIWindowScene) else { return } | |
/// 2. Create a new UIWindow using the windowScene constructor which takes in a window scene. | |
let window = UIWindow(windowScene: windowScene) | |
/// 3. Create a view hierarchy programmatically | |
let viewController = ArticleListViewController() | |
let navigation = UINavigationController(rootViewController: viewController) | |
/// 4. Set the root view controller of the window with your view controller | |
window.rootViewController = navigation | |
/// 5. Set the window and call makeKeyAndVisible() | |
self.window = window | |
window.makeKeyAndVisible() | |
} | |
[iOS] AppDelegate와 SceneDelegate
Deployment Target이 iOS 13 미만인 상황에서는?
- iOS13에서 새로 생긴 SceneDelegate.swift 파일 삭제
- iOS13에서 AppDelegate에 추가된 UISceneSession과 관련된 두 매소드 삭제
// MARK: UISceneSession Lifecycle
@available(iOS 13.0, *)
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)
}
@available(iOS 13.0, *)
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.
}
- iOS13에서 SceneDelegate로 옮겨진 window 프로퍼티를 AppDelegate로 다시 옮기기
var window: UIWindow?
- info.plist에서 Scene과 관련된 Manifest인 Application Scene Manifest 삭제
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to programmatically setup your app with Scene Delegate in Swift
Step 1/4: Remove the Main.storyboard file
You’ll want to remove the Main.storyboard file and get rid of the ViewController.swift file as you’ll most likely want to create your own initial view controller. At the very least you can just rename that class to something more relevant to your app.
Step 2/4: Remove 2 key value pairs in info.plist
You’ll then want to go ahead and delete the following key value pairs in your info.plist file:
and in AppleScript terminology they’re named UISceneStoryboardFile and UIMainStoryboardFile respectively
Step 3/4: Create your initial view controller
Step 4/4: Programmatically create your window hierarchy