193

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?

MattyG
  • 8,449
  • 6
  • 44
  • 48
Calvin
  • 8,697
  • 7
  • 43
  • 51

12 Answers12

314

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Reed Olsen
  • 9,099
  • 4
  • 37
  • 47
  • Excellent. Didn't think of using `NSNotificationCenter`. Thank you! – Calvin Sep 04 '10 at 01: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 '10 at 05: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 '11 at 16:59
  • 2
    @Aaron It can, but it's not a requirement. That's great insight, though. Thanks! – Reed Olsen Jan 31 '11 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 '12 at 01:28
  • brilliant! bah why didn't i think of that. spent hours trying to figure how to find out which controller was active from app controller.. thanks! – Khon Lieu Jun 19 '12 at 22:19
  • @ReedOlsen It isn't recommended to removeObserver:self. In this case you may remove observers, that you're inheriting. The only moment when you can do it, is when you know self will be retained. Better use the pattern to remove each observer separately. – Nat Aug 21 '13 at 13:02
  • How do I tell when my "view is going away"? – ChrisGNZ Jul 26 '18 at 00:58
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
}
igrek
  • 1,415
  • 1
  • 12
  • 27
  • 2
    where do I call this? –  Jan 03 '18 at 05: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 '18 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 '18 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.
}
Zorayr
  • 23,770
  • 8
  • 136
  • 129
  • Best place to put `removeObserver` in Swift: `deinit` method. – Enrico Susatyo May 16 '16 at 07: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 '16 at 20:23
  • 1
    Where would you removeObserver then? – Enrico Susatyo May 16 '16 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** – Dan Rosenstark Jun 01 '16 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, https://developer.apple.com/documentation/foundation/notificationcenter/1413994-removeobserver – Manoj Jan 02 '22 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
    }
Gurjinder Singh
  • 9,221
  • 1
  • 66
  • 58
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() {
}
Community
  • 1
  • 1
Abhishek Jain
  • 4,557
  • 2
  • 32
  • 31
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)
}
Josh Hinman
  • 6,745
  • 7
  • 38
  • 47
CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • 1
    `#selector` can call a method declared as an `@objc` attribute in Swift 4. – Anjan Biswas Apr 16 '18 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` – Yan Kalbaska Jul 25 '18 at 09: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 '20 at 09:00
  • Nevermind I also get the warning if I use the `@objc` variant. – palme Jan 29 '20 at 09: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.

Bogdan Razvan
  • 1,497
  • 1
  • 16
  • 16
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() {
    }
Naveen Sharma
  • 311
  • 2
  • 8
4

The Combine way:

import Combine

var cancellables = Set<AnyCancellable>()
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
    .sink { notification in
            // do stuff
    }.store(in: &cancellables)
0llie
  • 8,758
  • 2
  • 24
  • 13
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
}
budiDino
  • 13,044
  • 8
  • 95
  • 91
2

If any of you is using SwiftUI:

.onReceive(NotificationCenter.default.publisher(
    for: UIApplication.didBecomeActiveNotification)) { _ in
        print("DID BECOME ACTIVE")
    }
)
Rob
  • 2,243
  • 4
  • 29
  • 40
1

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

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