LoginSignup
30
26

More than 5 years have passed since last update.

【Swift】iOSアプリがフォアグラウンドになった時に、ViewControllerで更新処理をする

Last updated at Posted at 2016-07-17

これは何?

アプリがフォアグラウンドになった時に、表示内容を更新したいときの方法です。
バックグラウンドになった時も同じなので、いっしょにまとめました。

(標準の通知が使えますと教えていただいたので、最初の投稿で書いていたAppDelegateの部分は消しました。)

サンプル(Swift 3.0)

  • Xcode 8.2.1
  • Swift 3.0.2

ViewController.swift

表示内容を更新したいViewControllerに処理を追加します。

ViewController.swift
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.viewWillEnterForeground(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(ViewController.viewDidEnterBackground(_:)), name: NSNotification.Name.UIApplicationDidEnterBackground, object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func viewWillEnterForeground(_ notification: Notification?) {
        if (self.isViewLoaded && (self.view.window != nil)) {
            print("フォアグラウンド")
        }
    }

    func viewDidEnterBackground(_ notification: Notification?) {
        if (self.isViewLoaded && (self.view.window != nil)) {
            print("バックグラウンド")
        }
    }

}

viewDidLoad

NSNotificationCenterへ登録する

  • NSNotification.Name.UIApplicationWillEnterForegroundを受け取った時に、viewWillEnterForegroundを実行する設定
  • NSNotification.Name.UIApplicationDidEnterBackgroundを受け取った時に、viewDidEnterBackgroundを実行する設定

viewWillEnterForeground

フォアグラウンドになったときの処理を書く。
そのViewControllerが表示されている場合だけ、更新したいので、

    if (self.isViewLoaded && (self.view.window != nil))

のブロック内に処理を書きます。こうしないと、複数ViewControllerでNSNotificationCenter登録している場合、表示されていないViewControllerでも処理が実行されるので、条件付きにしています。

viewDidEnterBackground

バックグラウンドになったときの処理を書く。

サンプル(Swift 2.2)

Swift 2系の、投稿初期のサンプルです。

  • Xcode 7.3.1
  • Swift 2.2
ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.viewWillEnterForeground(_:)), name: UIApplicationWillEnterForegroundNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.viewDidEnterBackground(_:)), name: UIApplicationDidEnterBackgroundNotification, object: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func viewWillEnterForeground(notification: NSNotification?) {
        if (self.isViewLoaded() && (self.view.window != nil)) {
            print("フォアグラウンド")
        }
    }

    func viewDidEnterBackground(notification: NSNotification?) {
        if (self.isViewLoaded() && (self.view.window != nil)) {
            print("バックグラウンド")
        }
    }

}
30
26
4

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
30
26