App重启时如何使用方法?
How to use a method when an App is rebooted?
我目前正在使用 SwiftUI.
开发应用程序
我想在第4个进程重启应用时使用如下方法:
构建并运行这个project.Then一个控制台显示scene
sceneWillEnterForeground
onAppear
sceneDidBecomeActive
请按下主页按钮,应用程序进入后台。然后控制台显示 sceneWillResignActive
sceneDidEnterBackground
双击主页按钮并删除应用程序屏幕。然后控制台显示Message from debugger: Terminated due to signal 9
按应用程序图标并重新启动应用程序。然后控制台什么也没有显示,这里想用一个方法
但是我不能使用任何生命周期方法...
有什么办法吗?
代码如下:
SceneDelegate.swift
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
print("scene")
}
func sceneDidDisconnect(_ scene: UIScene) {
print("sceneDidDisconnect")
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("sceneDidBecomeActive")
}
func sceneWillResignActive(_ scene: UIScene) {
print("sceneWillResignActive")
}
func sceneWillEnterForeground(_ scene: UIScene) {
print("sceneWillEnterForeground")
}
func sceneDidEnterBackground(_ scene: UIScene) {
print("sceneDidEnterBackground")
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, World!")
}.onAppear(){
print("onAppear")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Xcode:版本 11.7
Swift: Swift 5
好吧,一旦您终止该应用程序,调试器也会断开与该应用程序的连接。因此无法在控制台中获取更多消息。您必须从 Xcode 重新启动应用程序,以便启动带有附加调试器的新实例。当您执行此操作时,您将返回到第 1 步。
您可以在应用进入后台时存储时间戳。然后,如果您终止应用程序并重新启动它,您可以在 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:)
方法中检查已经过去了多少时间。
如果最多过了 x 秒,您可以断定有人终止了该应用程序,然后决定重新启动它。如果超过 x 秒或没有存储时间戳,则表示它只是一个常规应用程序启动。
我目前正在使用 SwiftUI.
开发应用程序我想在第4个进程重启应用时使用如下方法:
构建并运行这个project.Then一个控制台显示
scene
sceneWillEnterForeground
onAppear
sceneDidBecomeActive
请按下主页按钮,应用程序进入后台。然后控制台显示
sceneWillResignActive
sceneDidEnterBackground
双击主页按钮并删除应用程序屏幕。然后控制台显示
Message from debugger: Terminated due to signal 9
按应用程序图标并重新启动应用程序。然后控制台什么也没有显示,这里想用一个方法
但是我不能使用任何生命周期方法...
有什么办法吗?
代码如下:
SceneDelegate.swift
import UIKit
import SwiftUI
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
print("scene")
}
func sceneDidDisconnect(_ scene: UIScene) {
print("sceneDidDisconnect")
}
func sceneDidBecomeActive(_ scene: UIScene) {
print("sceneDidBecomeActive")
}
func sceneWillResignActive(_ scene: UIScene) {
print("sceneWillResignActive")
}
func sceneWillEnterForeground(_ scene: UIScene) {
print("sceneWillEnterForeground")
}
func sceneDidEnterBackground(_ scene: UIScene) {
print("sceneDidEnterBackground")
}
}
ContentView.swift
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, World!")
}.onAppear(){
print("onAppear")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Xcode:版本 11.7
Swift: Swift 5
好吧,一旦您终止该应用程序,调试器也会断开与该应用程序的连接。因此无法在控制台中获取更多消息。您必须从 Xcode 重新启动应用程序,以便启动带有附加调试器的新实例。当您执行此操作时,您将返回到第 1 步。
您可以在应用进入后台时存储时间戳。然后,如果您终止应用程序并重新启动它,您可以在 application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions:)
方法中检查已经过去了多少时间。
如果最多过了 x 秒,您可以断定有人终止了该应用程序,然后决定重新启动它。如果超过 x 秒或没有存储时间戳,则表示它只是一个常规应用程序启动。