質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

3161閲覧

SceneDelegate.swiftを後から追加するには

R3.S

総合スコア44

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

1グッド

1クリップ

投稿2020/03/15 03:48

編集2020/03/15 03:49

実現したいこと

・SceneDelegate.swiftを後から追加したい。

現状

SceneDelegate.swiftがデフォルトで追加される前のverのXcodeでプログラムを作成していました。
新規で別のプロジェクトを作成して、SceneDelegate.swiftを移植先に移すとエラーがでます。

全体のソースコード

import UIKit import SwiftUI 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). // Create the SwiftUI view that provides the window contents. let contentView = ContentView() // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible() } } 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. } }

エラーメッセージ(一部)

イメージ説明

イメージ説明

試したこと

多すぎたのでひとまずFIXで直せるものは上から順に変更しました。

変更後のソースコード

import UIKit import SwiftUI class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? @available(iOS 13.0, *) 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). // Create the SwiftUI view that provides the window contents. let contentView = ContentView() // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene) window.rootViewController = UIHostingController(rootView: contentView) self.window = window window.makeKeyAndVisible() } } @available(iOS 13.0, *) 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). } @available(iOS 13.0, *) 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. } @available(iOS 13.0, *) 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). } @available(iOS 13.0, *) 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. } @available(iOS 13.0, *) 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. } }

エラー内容

イメージ説明

新規で作り直さないと厳しいのでしょうか。

s.k👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hoshi-takanori

2020/03/15 05:48 編集

そのアプリの対応 iOS バージョンは? また、SwiftUI を使用しますか?
R3.S

2020/03/15 06:13

hoshi-takanori様 コメントありがとうございます。 バージョンは12.4 SwiftUI はつかいません。
hoshi-takanori

2020/03/15 08:29

iOS 12.4 には SceneDelegate は存在しないので、iOS 12.4 では SceneDelegate は使わずに今まで通りの挙動、iOS 13 以降では SceneDelegate を使用するという場合分けのコードが必要になります。また、質問文の SceneDelegate は SwiftUI を使う前提のものなので、使わないのであればそのように修正(または、SwiftUI を使わない設定せ新規プロジェクトを生成して、その SceneDelegate を使用)する必要があります。自分はまだ SceneDelegate は詳しくないので、詳細な方法は分かりません。
R3.S

2020/03/15 09:03

hoshi-takanori様 ご返答ありがとうございます。 場合分けのコードが必要なのですね。。 もう少し考えてみます!いつもありがとうございます。
hoshi-takanori

2020/03/15 09:10

たぶん古い Xcode で作ったプロジェクトをそのまま新しい Xcode でビルドしても、しばらくは大丈夫だと思うので、iOS 12 の対応も必要であれば、急いで SceneDelegate に移行しなくても良いのではないかと思います。
guest

回答1

0

自己解決

現状、方法は見当たらなかったので作り直しました。

投稿2020/05/31 01:45

R3.S

総合スコア44

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問