ios 检测iOS应用进入后台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34744783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:34:08  来源:igfitidea点击:

Detect iOS app entering background

iosiphoneswift

提问by Johannes Flood

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 编码的 iOS 游戏。我试图找到一种方法来检测应用程序何时进入后台模式或因其他原因而中断,例如一个电话但找不到任何东西。我该怎么做?

回答by Leo Dabus

You can add an observer to your view controller:

您可以将观察者添加到您的视图控制器:

edit/update: Xcode 11 ? Swift 5

编辑/更新:Xcode 11?斯威夫特 5

iOS13 or later

iOS13 或更高版本

UIScene.willDeactivateNotification

iOS12 or earlier

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
}

回答by dimo hamdy

Swift3

斯威夫特3

let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: Notification.Name.UIApplicationWillResignActive, object: nil)


func appMovedToBackground() {
    print("App moved to background!")
}

回答by Rubaiyat Jahan Mumu

In swift 4 and iOS 12: To observe app enters backgroundevent, add this code to your viewDidLoad()method.

在 swift 4 和 iOS 12 中:要观察应用程序进入后台事件,请将此代码添加到您的viewDidLoad()方法中。

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)

    @objc func appMovedToBackground() {
        // do whatever event you want
    }

you have to use UIApplication.didEnterBackgroundNotification. If you want to observe if app came to foregroundevent, use UIApplication.willEnterForegroundNotification

你必须使用UIApplication.didEnterBackgroundNotification。如果你想观察 app 是否进入前台事件,使用UIApplication.willEnterForegroundNotification

So, the full code will be:

所以,完整的代码将是:

override func viewDidLoad() {
    super.viewDidLoad()

    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)

    notificationCenter.addObserver(self, selector: #selector(appCameToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)

    // Do any additional setup after loading the view.
}
 @objc func appMovedToBackground() {
    print("app enters background")
}

@objc func appCameToForeground() {
    print("app enters foreground")
}

回答by Suresh Kumar Durairaj

To detect the app enters background, you can check in the appDelegate.m find the application delegate method

检测应用进入后台,可以在appDelegate.m中查看应用委托方法

applicationDidEnterBackground

applicationDidEnterBackground

This method will get called, once the app enters background.

一旦应用程序进入后台,此方法将被调用。

回答by Karmadon

SwiftUI

用户界面

From background

从背景

Text("Hello, World!")
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
    print("To the foreground!")
}

To the background

到背景

Text("Hello, World!")
    .onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
        print("To the background!")
    }

回答by Alex Blundell

Take a look at the delegate methods defined in your instance of UIApplicationDeletegate(called AppDelegate.mby default). Specifically the following would be useful:

查看您的实例中定义的委托方法UIApplicationDeletegateAppDelegate.m默认调用)。具体来说,以下内容会很有用:

- (void)applicationWillResignActive:(UIApplication *)application

This method is called to let your app know that it is about to move from the active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the app and it begins the transition to the background state. An app in the inactive state continues to run but does not dispatch incoming events to responders.

调用此方法是为了让您的应用程序知道它即将从活动状态转变为非活动状态。这可能发生在某些类型的临时中断(例如来电或短信)或用户退出应用程序并开始转换到后台状态时。处于非活动状态的应用程序继续运行,但不会将传入事件分派给响应者。

Taken from the Apple Documentation - here

取自 Apple 文档 -此处