Swift applicationDidBecomeActive观察者UIApplicationDidBecomeActive未被调用

时间:2017-06-01 16:55:28

标签: ios swift swift3

我正在尝试解决以下问题: - 如果用户强制退出应用程序然后在强制退出后进入应用程序,我想从Core Data中获取一些数据,然后在视图控制器上显示它。

我试过使用以下观察者:

notification.addObserver(self, selector: #selector(reloadTimerOnAppStart), 
name: NSNotification.Name.UIApplicationDidBecomeActive, 
object: nil)

每次加载应用程序时,此观察者都会工作,但应用程序第一次加载时除外。

e.g。 - 强制退出应用程序 - 运行应用程序(没有任何反应) - 关闭应用程序 - 打开应用程序(选择器工作)

它第一次无法正常工作。

我是否在错误的地方实例化了这个?它应该发生在appDelegate:

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    // check if db has any outstanding running tasks
    // if it does then start the singleton timer
    // display it accordingly on the view.       
} 

根据这里最热烈的答案applicationWillEnterForeground vs. applicationDidBecomeActive, applicationWillResignActive vs. applicationDidEnterBackground

应该调用applicationDidBecomeActive,所以我只是不确定发生了什么。

在另一个注释中,如果你将UIAlert放在appDelegate.swift文件的这个函数中

func applicationDidBecomeActive(_ application: UIApplication) {
    AlertUser()
    // with UIAlertController //
}  

然后每次出现此警报!即使是在应用程序强制退出后的第一次加载,希望能帮助我解决这个问题,以便我能够实际为视图控制器工作。

1 个答案:

答案 0 :(得分:1)

我认为该方法无法调用,因为您正在强制关闭应用,这会打破应用的生命周期,这可能对您没有多大帮助,但想一想。