检测iOS应用进入后台 [英] Detect iOS app entering background
本文介绍了检测iOS应用进入后台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一款用 Swift 编码的 iOS 游戏.我试图找到一种方法来检测应用程序何时进入后台模式或因其他原因而中断,例如一个电话但找不到任何东西.我该怎么做?
I'm working on a game for iOS coded in Swift. I've tried to find a way to detect when the app enters background mode or is interrupted for other reasons, for example a phone call but can't find anything. How do I do it?
推荐答案
您可以向视图控制器添加观察者:
You can add an observer to your view controller:
编辑/更新:Xcode 11 • Swift 5
iOS13 或更高版本
UIScene.willDeactivateNotification
iOS12 或更早版本
UIApplication.willResignActiveNotification
<小时>
if #available(iOS 13.0, *) {
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIScene.willDeactivateNotification, object: nil)
} else {
NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: UIApplication.willResignActiveNotification, object: nil)
}
并向您的视图控制器添加一个选择器方法,该方法将在您的应用收到该通知时执行:
and add a selector method to your view controller that will be executed when your app receives that notification:
@objc func willResignActive(_ notification: Notification) {
// code to execute
}
这篇关于检测iOS应用进入后台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文