检测iOS应用程序进入后台 [英] Detect iOS app entering background

查看:96
本文介绍了检测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?

推荐答案

Swift 3或更高版本

您可以将观察者添加到视图控制器中 UIApplicationWillResignActiveNotification

You can add an observer to your view controller for UIApplicationWillResignActiveNotification

NotificationCenter.default.addObserver(self, selector: #selector(willResignActive), name: .UIApplicationWillResignActive, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆