Closed3

【Swift】AppDelegate 0->1

yoshitakayoshitaka

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        return true
    }

アプリケーション起動後のカスタマイズ用のオーバーライドポイント。


UISceneSession Lifecycle

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>) {
    }

ユーザーがシーンセッションを破棄したときに呼び出されます。

アプリケーションの実行中にセッションが破棄された場合、これはapplication:didFinishLaunchingWithOptionsの直後に呼び出されます。

このメソッドを使用して、破棄されたシーンに固有のリソースを解放します。リソースは戻らないためです。

yoshitakayoshitaka

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).
    guard (scene as? UIWindowScene) != nil else { return }
}

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 necessarily 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.

    // Save changes in the application's managed object context when the application transitions to the background.
    (UIApplication.shared.delegate as? AppDelegate)?.saveContext()
}
yoshitakayoshitaka

import UIKit
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
private var appCoordinator: AppCoordinator?

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions
    launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {

    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window

    let appCoordinator = AppCoordinator(window: window)
    appCoordinator.start()
    self.appCoordinator = appCoordinator
    return true
}

func application(_ app: UIApplication,
                 open url: URL,
                 options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window

    let type: AppCoordinator.LaunchType = .openURL(url)
    let appCoordinator = AppCoordinator(window: window,
                                        launchType: type)
    appCoordinator.start()
    self.appCoordinator = appCoordinator
    return true
}

func application(_ application: UIApplication,
                 continue userActivity: NSUserActivity,
                 restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window

    let type: AppCoordinator.LaunchType = .userActivity(userActivity)
    let appCoordinator = AppCoordinator(window: window,
                                        launchType: type)
    appCoordinator.start()
    self.appCoordinator = appCoordinator
    return true
}

func application(_ application: UIApplication,
                 performActionFor shortcutItem: UIApplicationShortcutItem,
                 completionHandler: @escaping (Bool) -> Void) {
    let window = UIWindow(frame: UIScreen.main.bounds)
    self.window = window
    let type: AppCoordinator.LaunchType = .shortcutItem(shortcutItem)
    let appCoordinator = AppCoordinator(window: window,
                                        launchType: type)
    appCoordinator.start()
    self.appCoordinator = appCoordinator
    completionHandler(true)
}

}

extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler
completionHandler: @escaping () -> Void) {
let window = UIWindow(frame: UIScreen.main.bounds)
self.window = window

    let request = response.notification.request
    let launchType: AppCoordinator.LaunchType = .notification(request)

    let appCoordinator = AppCoordinator(window: window,
                                        launchType: launchType)
    appCoordinator.start()
    self.appCoordinator = appCoordinator
    completionHandler()
}

}

このスクラップは2021/05/01にクローズされました