我想知道是否有一种方法或方法可用于操作一段代码,并且从后台调用app并变为活动状态。我需要从视图本身开始工作,而不是应用委托
答案 0 :(得分:2)
当您需要在从后台唤醒时在视图控制器中执行某些操作时,您必须使用 UIApplicationDidBecomeActiveNotification 的侦听器
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(self.executeMethod), name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIApplicationDidBecomeActive, object: nil)
}
func executeMethod() {
// execute your code here
}