【发布时间】:2021-08-22 18:56:46
【问题描述】:
这是我制作的一个简单的AppDelegate:
import SwiftUI
class AppDelegate: UIResponder, UIApplicationDelegate {
private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
print("application")
// first launch
// this method is called only on first launch when app was closed / killed
return true
}
private func applicationWillEnterForeground(application: UIApplication) -> Bool{
print("applicationWillEnterForeground")
// app will enter in foreground
// this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call)
return true
}
private func applicationDidBecomeActive(application: UIApplication) -> Bool {
print("applicationDidBecomeActive")
// app becomes active
// this method is called on first launch when app was closed / killed and every time app is reopened or change status from background to foreground (ex. mobile call)
return true
}
}
@main
struct CouponDeckApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@Environment(\.scenePhase) private var scenePhase
var body: some Scene {
WindowGroup {
AppContentView()
}
}
}
不过,AppDelegate 似乎没有调用它的任何函数。它应该在 3 个地方运行它们,但没有一个在运行。为什么会发生这种情况,我该如何解决?
【问题讨论】:
-
在 SwiftUI 生命周期中,
applicationWillEnterForeground和applicationDidBecomeActive似乎没有被调用(但是,您也没有正确的方法签名——使用自动完成来查看他们实际上应该是)。 @swiftPunk 已经回答了你提出的问题之一,向你展示了如何在 SwiftUI 中执行此操作。当您第一次在这里询问时,我还为您链接到此信息:stackoverflow.com/questions/68716601/… -
您似乎遗漏了一些要点!你以不同的形式问同样的问题!
-
@jnpdx 在 Apple 文档中,我似乎是正确的。名字完全匹配。
-
@swiftPunk 那是因为任何人给我的答案都没有奏效。
-
@KunalKatiyar 不,他们没有。检查返回类型。
标签: swiftui appdelegate