Background周りのNSNotificationまとめ

バックグラウンド -> アクティブ

willEnterForegroundNotification

willEnterForegroundNotificationはバックグラウンド状態(ホーム画面/Spring Board)から復帰する直前の通知です。

この通知は(ホーム画面/Spring Board)からアプリに復帰する時のみ呼ばれます

NotificationCenter.default.addObserver(self,
                                       selector: #selector(softInfoPreviewSoftTagWillAppear),
                                       name: UIApplication.willEnterForegroundNotification,
                                       object: nil)

didBecomeActiveNotification

onDidBecomeActiveはバックグラウンドから復帰して完全にアクティブな状態になった時の通知です。

この通知は以下の4パターンの遷移で通知されます

  1. ホーム画面(Spring Board)からアプリへの遷移
  2. Control Centerからアプリへの遷移
  3. Notification Centerからアプリへの遷移
  4. App Switcherからアプリへの遷移

NotificationCenter.default.addObserver(self,
                                       selector: #selector(onDidBecomeActive),
                                       name: UIApplication.didBecomeActiveNotification,
                                       object: nil)

アクティブ -> バックグラウンド

willResignActiveNotification

willResignActiveNotificationはアプリがアクティブな状態を他のアプリに渡す際に呼ばれます。バックグラウンドになる直前に呼ばれる通知だと考えて良いと思います。

この通知は以下の4パターンの遷移で通知されます

NotificationCenter.default.addObserver(self,
                                       selector: #selector(willResignActiveNotification),
                                       name: UIApplication.willResignActiveNotification,
                                       object: nil)

didEnterBackgroundNotification

didEnterBackgroundNotificationは完全にバックグラウンド状態になった時呼ばれる通知です。

この通知はアプリ画面からホーム画面(SpringBoard)に遷移する時のみ通知が呼ばれます。

NotificationCenter.default.addObserver(self,
                                       selector: #selector(onDidEnterBackground),
                                       name: UIApplication.didEnterBackgroundNotification,
                                       object: nil)

前の記事

Slide to unlickのUIを自作する

次の記事

CALayerでUIImageを表示する