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

倾然丶 夕夏残阳落幕 提交于 2019-11-26 22:03:13

问题


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.


回答1:


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



回答2:


Swift 4.2

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



回答3:


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




回答4:


Swift 3 and 4 version

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



回答5:


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
   }


来源:https://stackoverflow.com/questions/24910765/ios-nsnotificationcenter-to-check-whether-the-app-came-from-background-to-foregr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!