LoginSignup
2
1

More than 3 years have passed since last update.

今までStoryboardしか使っていないプロジェクトで、SwiftUIをとりあえず表示させるまで。

Posted at

概要

一年前に作ってたアプリの開発を再開させたい + SwiftUIに感動したので使いたい となった時にとりあえずSwiftUIを表示させる。

SceneDelegate.swiftを作る

とりあえず、新しくSwiftUIでプロジェクトを作った時にできるのと同じようにする。

SceneDelegate.swift

import UIKit
import SwiftUI

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: Home())
            self.window = window
            window.makeKeyAndVisible()
        }
    }
}

この時点ではアプリに変化はない。

window.rootViewController = UIHostingController(rootView: Home())という行がありますが、SwiftUIでHome.swiftを作っておく必要があるので注意。Hello Worldの表示を作ると良いと思います。

この時点でSwiftUIのResumeはできると思いますので、一応描画しておくと良いです。

Home.swift
import SwiftUI

struct Home: View {
    var body: some View {
        Text("Hello Shohei-kun!")
    }
}

struct Home_Previews: PreviewProvider {
    static var previews: some View {
        Home()
    }
}

AppDelegate.swiftの変更

とりあえず、今までのコードを以下に変更。

この時、既存のコードでAppDelegateでのカスタマイズをしている場合は必要な関数など消えないように注意してください。

僕の場合は、Widgetがあるので、
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool
などが残っていますが、無事動いています。

AppDelegate.swift
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        return true
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    // 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.
    }
}

この時点で動かすとSimulatorとか真っ黒な画面になりパニくる。

info.plistの変更

とりあえず、info.plistに移動し、Information Property ListにApplication Scene Manifestを追加して、以下になるように調整。

スクリーンショット 2020-07-23 0.43.36.png

スクリーンショット 2020-07-23 0.44.02.png

無事動いた!

Xcode Version 11.4.1 (11E503a)
以上。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1