194

I have the UIApplicationDelegate protocol in my main AppDelegate.m class, with the applicationDidBecomeActive method defined.

I want to call a method when the application returns from the background, but the method is in another view controller. How can I check which view controller is currently showing in the applicationDidBecomeActive method and then make a call to a method within that controller?

12 Answers 12

315

Any class in your application can become an "observer" for different notifications in the application. When you create (or load) your view controller, you'll want to register it as an observer for the UIApplicationDidBecomeActiveNotification and specify which method that you want to call when that notification gets sent to your application.

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(someMethod:)
                                             name:UIApplicationDidBecomeActiveNotification object:nil];

Don't forget to clean up after yourself! Remember to remove yourself as the observer when your view is going away:

[[NSNotificationCenter defaultCenter] removeObserver:self 
                                                name:UIApplicationDidBecomeActiveNotification
                                              object:nil];

More information about the Notification Center.

8
  • Excellent. Didn't think of using NSNotificationCenter. Thank you!
    – Calvin
    Sep 4, 2010 at 1:33
  • 3
    Just a typo in that line of code (missing 'name'): [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(someMethod:) name:UIApplicationDidBecomeActiveNotification object:nil];
    – Johnus
    Nov 12, 2010 at 5:55
  • 3
    To add to Reed's answer, the method that is called (in this example it's someMethod) needs to accept an NSNotification parameter. So the method signature for someMethod would be -(void)someMethod:(NSNotification *)notification { //Do Something Here }
    – Aaron
    Jan 31, 2011 at 16:59
  • 2
    @Aaron It can, but it's not a requirement. That's great insight, though. Thanks!
    – Reed Olsen
    Jan 31, 2011 at 22:33
  • Fantastic! What a great way to invalidate / recreate NSTimer instances one has going, right in the view controllers / other objects that are responsible for those NSTimers. Love it!
    – idStar
    Jan 28, 2012 at 1:28
87

Swift 3, 4 Equivalent:

adding observer

NotificationCenter.default.addObserver(self,
    selector: #selector(applicationDidBecomeActive),
    name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
    object: nil)

removing observer

NotificationCenter.default.removeObserver(self,
    name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
    object: nil)

callback

@objc func applicationDidBecomeActive() {
    // handle event
}
3
  • 2
    where do I call this?
    – user8169082
    Jan 3, 2018 at 5:23
  • 1
    @user8169082, you add an observer wherever you need to start receiving notifications. You could add it on viewDidLoad or viewWillAppear:animated for instance. And you can remove an observer when you no longer need notifications, or when your observer instance is going to be deallocated in the deinit method
    – igrek
    Feb 20, 2018 at 15:35
  • 2
    swift 4.2 I am using: NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive(notification:)), name: UIApplication.didBecomeActiveNotification, object: nil)
    – Brian Bird
    Oct 12, 2018 at 17:27
16

Swift 2 Equivalent:

let notificationCenter = NSNotificationCenter.defaultCenter()

// Add observer:
notificationCenter.addObserver(self,
  selector:Selector("applicationWillResignActiveNotification"),
  name:UIApplicationWillResignActiveNotification,
  object:nil)

// Remove observer:
notificationCenter.removeObserver(self,
  name:UIApplicationWillResignActiveNotification,
  object:nil)

// Remove all observer for all notifications:
notificationCenter.removeObserver(self)

// Callback:
func applicationWillResignActiveNotification() {
  // Handle application will resign notification event.
}
5
  • Best place to put removeObserver in Swift: deinit method. May 16, 2016 at 7:29
  • Generally, accessing self in deinit is not advised; at this point, self is in between being fully allocated and being deallocated
    – Zorayr
    May 16, 2016 at 20:23
  • 1
    Where would you removeObserver then? May 16, 2016 at 23:51
  • 2
    @EnricoSusatyo you can ignore that, as it's not correct. Overriding deinit is fine: "Because an instance is not deallocated until after its deinitializer is called, a deinitializer can access all properties of the instance it is called on and can modify its behavior based on those properties (such as looking up the name of a file that needs to be closed)." Calling deinit is not ok Jun 1, 2016 at 20:20
  • 1
    If your app targets iOS 9.0 and later or macOS 10.11 and later, you don't need to remove the observer. The system cleans it. For reference, developer.apple.com/documentation/foundation/notificationcenter/…
    – Manoj
    Jan 2, 2022 at 13:07
14

Swift 5

fileprivate  func addObservers() {
      NotificationCenter.default.addObserver(self,
                                             selector: #selector(applicationDidBecomeActive),
                                             name: UIApplication.didBecomeActiveNotification,
                                             object: nil)
    }

fileprivate  func removeObservers() {
        NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
    }

@objc fileprivate func applicationDidBecomeActive() {
// here do your work
    }
0
7

Swift 4.2

Add observer-

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

Remove observer-

NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)

Handle Event-

@objc func handleEvent() {
}
6

With Swift 4, Apple advises via a new compiler warning that we avoid the use of #selector in this scenario. The following is a much safer way to accomplish this:

First, create a variable that will hold the observer instance (that will be used to cancel it):

var didBecomeActiveObserver: NSObjectProtocol

Then create a lazy var that can be used by the notification:

lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
    // Do stuff
} 

If you require the actual notification be included, just replace the _ with notification.

Next, we set up the notification to observe for the app becoming active.

func setupObserver() {
    didBecomeActiveObserver = NotificationCenter.default.addObserver(
                                  forName: UIApplication.didBecomeActiveNotification,
                                  object: nil,
                                  queue:.main,
                                  using: didBecomeActive)
}

The big change here is that instead of calling a #selector, we now call the var created above. This can eliminate situations where you get invalid selector crashes.

Finally, we remove the observer.

func removeObserver() {
    NotificationCenter.default.removeObserver(didBecomeActiveObserver)
}
4
  • 1
    #selector can call a method declared as an @objc attribute in Swift 4. Apr 16, 2018 at 23:11
  • 1
    it is incorrect to use removeObserver(self because self was not assigned when adding observer. You should let observer = NotificationCenter.default.addObserver then removeObserver(observer Jul 25, 2018 at 9:42
  • Thanks @CodeBender I did not know that function yet and it (finally) removes the @objc. However when I try it I get a warning in the console (Xcode 11.3.1 (11C504), Swift 13.3): Can't end BackgroundTask: no background task exists with identifier. Even if I save the observer in a variable as NSObjectProtocol.
    – palme
    Jan 29, 2020 at 9:00
  • Nevermind I also get the warning if I use the @objc variant.
    – palme
    Jan 29, 2020 at 9:21
5

Swift 5 version:

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

Removing the observer is no longer required in iOS 9 and later.

5

In Swift 5

override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)

         NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
    
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
        }
    
    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)
    }


@objc private func applicationWillResignActive() {
    }

    @objc private func applicationDidBecomeActive() {
    }
4

The Combine way:

import Combine

var cancellables = Set<AnyCancellable>()
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
    .sink { notification in
            // do stuff
    }.store(in: &cancellables)
3

Cleaner Swift 5+ solution

Add the observer to init or viewDidLoad:

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

You don't need to remove the observer as other answers suggest. It will be done automatically.

@objc private func appDidBecomeActive() {
    // do your magic
}
2

If any of you is using SwiftUI:

.onReceive(NotificationCenter.default.publisher(
    for: UIApplication.didBecomeActiveNotification)) { _ in
        print("DID BECOME ACTIVE")
    }
)
1

For Swift5 MacOS, you need to use NSApplication instead of UIApplication.

NotificationCenter.default.addObserver(self,
                                       selector: #selector(applicationDidBecomeActive),
                                       name: (NSApplication.didBecomeActiveNotification),
                                       object: nil)
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.