iOS NSNotificationCenter 检查应用程序是否从后台到前台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24910765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:30:45  来源:igfitidea点击:

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

iosbackgroundnsnotificationcenterforeground

提问by user3115014

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.

我有一种情况,每次从背景到前景时,我都必须初始化一个对象,并且应该使用 NSNotificationCenter 而不是 appdelegate,因为我正在构建一个静态库,因此不会有 appdelegate,所以请帮助我.

回答by Nikita Took

Have you tried UIApplicationWillEnterForegroundNotification?

你试过UIApplicationWillEnterForegroundNotification吗?

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

该应用程序还会在调用之前不久发布 UIApplicationWillEnterForegroundNotification 通知applicationWillEnterForeground:,让感兴趣的对象有机会响应转换。

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];

回答by Andrea Miotto

Swift 4.2

斯威夫特 4.2

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

回答by mt81

Swift 3 version

斯威夫特 3 版本

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

你也可以使用 NSNotification.Name.UIApplicationDidBecomeActive

回答by Kiran Jasvanee

Swift 5

斯威夫特 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
    }

回答by omerx

Swift 3 and 4 version

Swift 3 和 4 版本

NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationWillEnterForeground, object: nil, queue: nil) { notification in
        ...
}

回答by Gurjinder Singh

swift 4.1

快速 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
   }