Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when a view controller goes to background and gets resumed

Tags:

ios

swift

swift3

I'm implementing a ViewController with the following requirement: If the user returns to the app after 15 minutes, the view should reload the data.

I was thinking on using viewDidDisappear to save the timestamp when the app went to background and viewDidAppear for checking previously saved values and refresh if needed, but this methods are not getting called when switching between apps.

How can I solve this in a easy way?

like image 878
Addev Avatar asked Aug 30 '17 08:08

Addev


1 Answers

use UIApplicationDidBecomeActive for resume and UIApplicationWillResignActive for handle goes background

SwiftUI

Text("check application state!")
                .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
                    print("User received on  willResignActiveNotification!")
                }
                .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
                    print("User received on  didBecomeActiveNotification!")
                }

Swift 5.x > above

override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)
        NotificationCenter.default.removeObserver(self, name:  UIApplication.didBecomeActiveNotification, object: nil)
    }

override func viewDidLoad() {
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(self.openAndCloseActivity), name: UIApplication.willResignActiveNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.openAndCloseActivity), name: UIApplication.didBecomeActiveNotification, object: nil)
}

@objc func openAndCloseActivity(_ notification: Notification)  {
    if notification.name == UIApplication.didBecomeActiveNotification{
        // become active notifictaion
    }else{
        // willResignActiveNotification
    }
    
}

Swift 5.x < below

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
     NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    NotificationCenter.default.addObserver(self, selector: #selector(self.closeActivityController), name: NSNotification.Name.UIApplicationWillResignActive, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.openactivity), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
   
    
}

and handle the method as

func closeActivityController()  {
    
    
}

func openactivity()  {
    
    //view should reload the data.
}

other notification types are

extension NSNotification.Name { 
@available(iOS 4.0, *)
public static let UIApplicationDidEnterBackground: NSNotification.Name

@available(iOS 4.0, *)
public static let UIApplicationWillEnterForeground: NSNotification.Name

public static let UIApplicationDidFinishLaunching: NSNotification.Name

public static let UIApplicationDidBecomeActive: NSNotification.Name

public static let UIApplicationWillResignActive: NSNotification.Name

public static let UIApplicationDidReceiveMemoryWarning: NSNotification.Name

public static let UIApplicationWillTerminate: NSNotification.Name

}
like image 119
Anbu.Karthik Avatar answered Oct 08 '22 05:10

Anbu.Karthik