0
0

Storyboard `AppDelegate`について

Posted at

目的

プロジェクト作成時にすでに存在するAppDelegateについて気になったので少し調べました。
なるべくわかりやすいように書いていきます

前提条件

Xcode15.1

AppDelegateに関して

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

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

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


この記述をふかぼっていこうと思います。

UIApplicationDelegateとは?(プロトコル)

公式サイトにはA set of methods to manage shared behaviors for your app.と記述があり直訳するとアプリの共有の挙動を管理するための一連のメソッド
でした.

共有の挙動とは?

  • アプリ全体で共通して必要な動作や処理を指します。
  • 具体的にはアプリの起動 アプリの終了 バックグラウンドへの以降など

**UIApplicationDelegate**はプロトコルなのになんで公式に「メソッド」と書かれているのか。

  • UIApplicationDelegateがもつメソッドをさしているため
  • protocolというのは関数置き場みたいなもの(継承して実際の関数の処理を決める)

つまりUIApplicationDelegateとは上記挙動を管理するためのプロトコルです。(アプリのライフサイクルともいう)

UIResponderとは?(クラス)

公式サイトにはAn abstract interface for responding to and handling events.と記述。
直訳は「イベントに応答し、処理するための抽象的なインターフェース」

つまり何かの処理に応答し処理するためのクラスというわけです。

いよいよ中の関数について理解していきましょう

結論のみ

コメントアウトに記載しておきます。

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  // アプリ内で使用するデータベースの初期化や接続設定を行います。
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    return true
  }
  
  // アプリが複数の画面を持つ場合にどの設定を適用するかという記述
  // 標準の`Default Configuration`という設定を適用させている
  func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
  }
  // アプリ内で現在アクティブではない(働いてない)もしくは不要になった画面やウィンドウを閉じたときに呼ばれる
  // 呼ばれたら使ってないものを削除したりする
  // 掃除機みたいな感じ
  func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
  }
}

参考文献

公式サイト(UIApplicationDelegate)
https://developer.apple.com/documentation/uikit/uiapplicationdelegate

公式サイト(UIResponder)
https://developer.apple.com/documentation/uikit/uiresponder

0
0
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
0
0