UIApplication.didBecomeActiveNotification 未称为 iOS 14

Dev*_*B2F 5 swiftui ios14

我有一个带有 a 的项目SceneDelegate,并且我在 SwiftUI 视图中使用以下代码:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
    print("Did become active...")
}
Run Code Online (Sandbox Code Playgroud)

它在 iOS 13 中被调用,但当我在 iOS 14 上运行它时,它没有运行。

更多详细信息:didBecomeActiveNotification当我将应用程序放入后台后将其置于前台时调用,但当应用程序第一次打开时则不会调用。但在 iOS 13 中,它会在第一次打开调用时执行此操作。

我将 Xcode 从 12.2 更新到 12.3,问题仍然存在。didBecomeActiveNotification有人可以检查使用 iOS 14 打开应用程序时是否接到电话吗?

知道如何进行这项工作吗?

附言。要在 Xcode 12 上使用 SceneDelegate 创建项目AppName.app,应删除该文件,然后使用样板代码创建文件并编辑SceneDelegate.swift“应用程序场景清单”键以使其使用 SceneDelegate。AppDelegate.swiftinfo.plist

paw*_*222 4

我做了一些测试,看来你是对的。我不确定这是一个错误还是有意的更改。

对我来说唯一可行的解​​决方案是onReceive+ Just+ scenePhase

import Combine
import SwiftUI

struct ContentView: View {
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        Text("Test")
            .onReceive(Just(scenePhase)) {
                print("onReceive scenePhase \($0)")
            }
    }
}
Run Code Online (Sandbox Code Playgroud)

其他测试如下(Xcode 14.3、iOS 12.3):

struct ContentView: View {
    @Environment(\.scenePhase) private var scenePhase

    var body: some View {
        Text("Test")
            // not working on the first launch
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
                print("onReceive didBecomeActiveNotification")
            }
            // not working on the first launch
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
                print("onReceive willEnterForegroundNotification")
            }
            // not working on the first launch
            .onReceive(NotificationCenter.default.publisher(for: UIApplication.didFinishLaunchingNotification)) { _ in
                print("onReceive didFinishLaunchingNotification")
            }
            // **working** on the first launch
            .onReceive(Just(scenePhase)) {
                print("onReceive scenePhase \($0)")
            }
            // not working on the first launch
            .onChange(of: scenePhase) {
                print("onChange scenePhase \($0)")
            }
    }
}
Run Code Online (Sandbox Code Playgroud)