Skip to content

Instantly share code, notes, and snippets.

@zlq4863947
Last active February 21, 2018 01:04
Show Gist options
  • Save zlq4863947/d852326a57c22d07a2c5f6021833ce2f to your computer and use it in GitHub Desktop.
Save zlq4863947/d852326a57c22d07a2c5f6021833ce2f to your computer and use it in GitHub Desktop.
【Swift4】ViewControllerでアプリがバックグラウンド/フォアグラウンドになったことを検知する方法 ref: https://qiita.com/zlq4863947/items/2484bcdd12c90db45b01
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func applicationDidEnterBackground(_ application: UIApplication) {
// アプリがバックグラウンドへ移行するタイミングを通知
NotificationCenter.default.post(name: Notification.Name.UIApplicationDidEnterBackground, object: nil)
}
func applicationWillEnterForeground(_ application: UIApplication) {
// アプリがフォアグラウンドへ移行するタイミングを通知
NotificationCenter.default.post(name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
}
// AppDelegate -> applicationWillEnterForegroundの通知
@objc func viewWillEnterForeground(notification: Notification) {
print("フォアグラウンド")
}
// AppDelegate -> applicationDidEnterBackgroundの通知
@objc func viewDidEnterBackground(notification: Notification) {
print("バックグラウンド")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment