iOS NSNotificationCenter to check whether the app came from background to foreground

前端 未结 7 960
我在风中等你
我在风中等你 2020-12-07 00:41

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 ap

相关标签:
7条回答
  • 2020-12-07 00:49

    Swift 3 and 4 version

    NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillEnterForeground, object: nil, queue: nil) { notification in
            ...
    }
    
    0 讨论(0)
  • 2020-12-07 00:51

    Swift 5 use: UIApplication.willEnterForegroundNotification

    0 讨论(0)
  • 2020-12-07 00:54

    swift 4.1

     override func viewDidAppear(_ animated: Bool) {
            NotificationCenter.default.addObserver(self, selector: #selector(enterForeground), name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
        }
    
        override func viewDidDisappear(_ animated: Bool) {
            NotificationCenter.default.removeObserver(self, name: Notification.Name.UIApplicationWillEnterForeground, object: nil)
        }
    
        @objc func enterForeground() {
           // Do stuff
       }
    
    0 讨论(0)
  • 2020-12-07 01:04

    Swift 4.2

    NotificationCenter.default.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification
                , object: nil)
    
    0 讨论(0)
  • 2020-12-07 01:06

    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
        }
    
    0 讨论(0)
  • 2020-12-07 01:09

    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];
    
    0 讨论(0)
提交回复
热议问题