【问题标题】:iOS NSNotificationCenter to check whether the app came from background to foregroundiOS NSNotificationCenter 检查应用程序是否从后台到前台
【发布时间】:2014-09-14 15:41:49
【问题描述】:

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

【问题讨论】:

    标签: ios background nsnotificationcenter foreground


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      Swift 5 使用: UIApplication.willEnterForegroundNotification

      【讨论】:

        【解决方案3】:

        斯威夫特 5

        订阅通知 -

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

        删除订阅 -

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

        要调用的函数 -

        @objc func applicationWillEnterForeground(_ notification: NSNotification) {
               self.volumeSlider.value = AVAudioSession.sharedInstance().outputVolume
            }
        

        【讨论】:

          【解决方案4】:

          斯威夫特 4.2

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

          【讨论】:

            【解决方案5】:

            Swift 3 和 4 版本

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

            【讨论】:

              【解决方案6】:

              Swift 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) {
                 ....
              }
              

              你也可以使用NSNotification.Name.UIApplicationDidBecomeActive

              【讨论】:

                【解决方案7】:

                你试过UIApplicationWillEnterForegroundNotification吗?

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

                订阅通知:

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

                实现一个需要调用的代码:

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

                别忘了退订:

                [[NSNotificationCenter defaultCenter] removeObserver:self];
                

                【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-10-24
                • 2016-09-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-04-29
                相关资源
                最近更新 更多