アプリの状態が変わった時の処理

アプリの状態が変わった時の処理に呼び出されるメソッド

  1. applicationWillResignActive →
    アプリがアクティブになる直前の処理
  2. applicationDidEnterBackground →
    アプリがバックグランドになった時の処理
  3. applicationWillEnterForeground →
    アプリがフォアグランドになった時の処理
  4. applicationDidBecomeActive →
    アプリがアクティブになった時の処理
  5. applicationWillTerminate →
    アプリが終了される時の処理

主な使い方

・アプリがバックグランドになった際に処理を中断する

・フォアグランドになった時に、住所の位置情報を取得する

・アプリがアクティブになった時に、サーバーから最新のデータを取得する

サンプルソース

Xcodeで新規プロジェクト作成したタイミングで各メソッドは自動的に記述されているはず。
あとは、中身にしたい処理を埋め込むだけ

import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
/*
     * アプリの起動準備が終わった際に呼び出されるメソッド
     *
     * application:UIApplicationオブジェクト
     * launchOptions:オプション
     */
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// StoryBoardから初回表示画面となるビューコントローラーを取得
let stroyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = stroyboard.instantiateInitialViewController()
let subStroyboard = UIStoryboard(name: "Sub2", bundle: nil)
let subController = subStroyboard.instantiateInitialViewController()
window = UIWindow(frame: UIScreen.main.bounds)
// ルートビューコントローラーを指定
//        window?.rootViewController = controller
window?.rootViewController = subController
// ウィンドウを表示
window?.makeKeyAndVisible()
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
print("アプリがアクティブになる直前の処理")
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
print("アプリがバックグランドになった時の処理")
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
print("アプリがフォアグランドになった時の処理")
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
print("アプリがアクティブになった時の処理")
}
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
print("アプリが終了される時の処理")
}
}

【Git】
https://github.com/satoNobu/study_swift/commit/97d7cff7af5b3cad5827ca8ae73171afb33d95d0

ログは、Xcode上に表示される。

f:id:nananiku-s-60:20190122073925p:plain

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です