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

前端 未结 7 961
我在风中等你
我在风中等你 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条回答
  • 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

    0 讨论(0)
提交回复
热议问题