从自定义框架调用Appdelegate方法

时间:2018-04-05 11:47:04

标签: ios xcode frameworks appdelegate

我目前正在开发一个框架,通过该框架我想检查应用是否已经启动以及它是否在前台。

问题是UIApplication无法在框架中导入。有没有办法捕获该事件而无需在应用程序中执行此操作?

1 个答案:

答案 0 :(得分:0)

在我看来,不是访问AppDelegate,这是一种糟糕的编码风格,你可以改为倾听事件:

func startListen() {
    NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground(_:)), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
}

func stopListen() {
    NotificationCenter.default.removeObserver(self)
}

func applicationWillEnterForeground(_ notification: NSNotification) {
    print("App moved to foreground!")
}

还有更多通知类型,例如UIApplicationDidEnterBackground,可以通知您有关整个应用生命周期的信息。

相关问题