iOS NSNotificationCenter to check whether the app came from background to foreground
Asked Answered
R

7

38

I have a situation in which i have to intialize an object everytime when it comes from background to foreground and that should be using the NSNotificationCenter not with appdelegate because iam building a static library so there wont be appdelegate with that so please help me in the same.

Rostock answered 23/7, 2014 at 12:41 Comment(0)
P
47

Have you tried UIApplicationWillEnterForegroundNotification?

The app also posts a UIApplicationWillEnterForegroundNotification notification shortly before calling applicationWillEnterForeground: to give interested objects a chance to respond to the transition.

Subscribe to notification:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(yourUpdateMethodGoesHere:)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

Implement a code, that need to be called:

- (void) yourUpdateMethodGoesHere:(NSNotification *) note {
// code
}

Don't forget to unsubscribe:

[[NSNotificationCenter defaultCenter] removeObserver:self];
Penninite answered 23/7, 2014 at 12:44 Comment(1)
Check this one: #2192094Aeneous
G
29

Swift 4.2

NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification
            , object: nil)
Gonium answered 3/10, 2018 at 10:34 Comment(0)
E
19

Swift 5

Subscribe to Notification -

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

    NotificationCenter.default.addObserver(
      self,
      selector: #selector(applicationWillEnterForeground(_:)),
      name: UIApplication.willEnterForegroundNotification,
      object: nil)
}

Remove subscription -

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

        NotificationCenter.default.removeObserver(self)
} 

Function to be called -

@objc func applicationWillEnterForeground(_ notification: NSNotification) {
       self.volumeSlider.value = AVAudioSession.sharedInstance().outputVolume
    }
Eos answered 27/12, 2019 at 11:3 Comment(0)
I
12

Swift 3 version

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    NotificationCenter.default.addObserver(self,
                                           selector:#selector(applicationWillEnterForeground(_:)),
                                           name:NSNotification.Name.UIApplicationWillEnterForeground,
                                           object: nil)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)
}

func applicationWillEnterForeground(_ notification: NSNotification) {
   ....
}

you can also use NSNotification.Name.UIApplicationDidBecomeActive

Imperialism answered 29/11, 2016 at 16:53 Comment(0)
M
4

Swift 5 use: UIApplication.willEnterForegroundNotification

Mirellamirelle answered 8/10, 2020 at 18:56 Comment(0)
Y
3

swift 5

 override func viewDidAppear(_ animated: Bool) {
    NotificationCenter.default.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
    }
    
    override func viewDidDisappear(_ animated: Bool) {
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: UIApplication.willEnterForegroundNotification.rawValue), object: nil)
    }

    @objc func appMovedToForeground() {
       // Do stuff
   }
Ybarra answered 22/9, 2018 at 17:48 Comment(0)
J
2

Swift 3 and 4 version

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillEnterForeground, object: nil, queue: nil) { notification in
        ...
}
Jin answered 15/4, 2018 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.