假设App进入后台,停止Download数据,那如何在App重新进入前台时,继续执行Download的操作呢?
首先我们要在相应的Controller设置一个监听者,负责监听App进入前台时发出的消息--UIApplication.willEnterForegroundNotification,当监听到响应的消息时,调用我们想要继续执行的操作。
override func viewDidLoad() {
super.viewDidLoad()
// Add an observer for app will enter foreground
NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground(notification:)), name: UIApplication.willEnterForegroundNotification, object: nil)
}
记得Controller的生命周期结束时,要释放监听者
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// Remove notification observer
NotificationCenter.default.removeObserver(self)
}