处理应用程序 DidBecomeActive ——“视图控制器如何响应应用程序变为 Active?”

我在我的主 Appgenerate.m 类中定义了 UIApplicationDelegate协议,并定义了 applicationDidBecomeActive方法。

我想在应用程序从后台返回时调用一个方法,但该方法位于另一个视图控制器中。如何检查当前在 applicationDidBecomeActive方法中显示的视图控制器,然后调用该控制器中的方法?

105498 次浏览

应用程序中的任何类都可以成为应用程序中不同通知的“观察者”。在创建(或加载)视图控制器时,需要将其注册为 UIApplicationDidBecomeActiveNotification的观察器,并指定在通知发送到应用程序时要调用的方法。

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(someMethod:)
name:UIApplicationDidBecomeActiveNotification object:nil];

不要忘记清理自己的垃圾! 记住,当你的观点消失的时候,不要让自己成为旁观者:

[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:nil];

关于 通知中心的更多信息。

Swift 2等效 :

let notificationCenter = NSNotificationCenter.defaultCenter()


// Add observer:
notificationCenter.addObserver(self,
selector:Selector("applicationWillResignActiveNotification"),
name:UIApplicationWillResignActiveNotification,
object:nil)


// Remove observer:
notificationCenter.removeObserver(self,
name:UIApplicationWillResignActiveNotification,
object:nil)


// Remove all observer for all notifications:
notificationCenter.removeObserver(self)


// Callback:
func applicationWillResignActiveNotification() {
// Handle application will resign notification event.
}

Swift 3,4等效:

增加观察者

NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)

移除观察者

NotificationCenter.default.removeObserver(self,
name: .UIApplicationDidBecomeActive, // UIApplication.didBecomeActiveNotification for swift 4.2+
object: nil)

复试

@objc func applicationDidBecomeActive() {
// handle event
}

在 Swift 4中,苹果通过一个新的编译器警告我们在这个场景中要避免使用 #selector。以下是实现这一目标的更安全的方法:

首先,创建一个保存观察者实例(用于取消它)的变量:

var didBecomeActiveObserver: NSObjectProtocol

然后创建一个可以被通知使用的惰性变量:

lazy var didBecomeActive: (Notification) -> Void = { [weak self] _ in
// Do stuff
}

如果您要求包含实际的通知,只需将 _替换为 notification

接下来,我们设置通知来观察应用程序是否处于活动状态。

func setupObserver() {
didBecomeActiveObserver = NotificationCenter.default.addObserver(
forName: UIApplication.didBecomeActiveNotification,
object: nil,
queue:.main,
using: didBecomeActive)
}

这里最大的变化是,我们现在不再调用 #selector,而是调用上面创建的 var。这可以消除无效选择器崩溃的情况。

最后,我们移除观察者。

func removeObserver() {
NotificationCenter.default.removeObserver(didBecomeActiveObserver)
}

Swift 4.2

加入观察员-

NotificationCenter.default.addObserver(self, selector: #selector(handleEvent), name: UIApplication.didBecomeActiveNotification, object: nil)

删除观察者-

NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)

处理事件-

@objc func handleEvent() {
}

Swift 5

fileprivate  func addObservers() {
NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)
}


fileprivate  func removeObservers() {
NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
}


@objc fileprivate func applicationDidBecomeActive() {
// here do your work
}

结合方式:

import Combine


var cancellables = Set<AnyCancellable>()
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
.sink { notification in
// do stuff
}.store(in: &cancellables)

Swift 5 版本:

 NotificationCenter.default.addObserver(self,
selector: #selector(loadData),
name: UIApplication.didBecomeActiveNotification,
object: nil)

在 iOS9和更高版本中移除观察器 不再需要了

如果你们中的任何人正在使用 SwiftUI:

.onReceive(NotificationCenter.default.publisher(
for: UIApplication.didBecomeActiveNotification)) { _ in
print("DID BECOME ACTIVE")
}
)

在 Swift 5里

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)


NotificationCenter.default.addObserver(self, selector: #selector(applicationWillResignActive), name: UIApplication.willResignActiveNotification, object: nil)
    

NotificationCenter.default.addObserver(self, selector: #selector(applicationDidBecomeActive), name: UIApplication.didBecomeActiveNotification, object: nil)
}
    

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)


NotificationCenter.default.removeObserver(self, name: UIApplication.willResignActiveNotification, object: nil)


NotificationCenter.default.removeObserver(self, name: UIApplication.didBecomeActiveNotification, object: nil)
}




@objc private func applicationWillResignActive() {
}


@objc private func applicationDidBecomeActive() {
}

对于 Swift5 MacOS,需要使用 NSApplication 而不是 UIApplication。

NotificationCenter.default.addObserver(self,
selector: #selector(applicationDidBecomeActive),
name: (NSApplication.didBecomeActiveNotification),
object: nil)
}

清洁雨燕5 + 解决方案

将观察器添加到 initviewDidLoad:

NotificationCenter.default.addObserver(self,
selector: #selector(appDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
object: nil)

你不需要像其他答案所暗示的那样移除观察者。

@objc private func appDidBecomeActive() {
// do your magic
}