gpt4 book ai didi

swiftui - UIApplication.didBecomeActiveNotification 未调用 iOS 14

转载 作者:行者123 更新时间:2023-12-04 03:43:01 47 4
gpt4 key购买 nike

我有一个项目 SceneDelegate我在 SwiftUI View 中使用以下代码:

.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
print("Did become active...")
}
它在 iOS 13 中被调用,但是当我在 iOS 14 上运行它时它不会运行。
更多详情: didBecomeActiveNotification当我将应用程序置于后台后将其置于前台时调用,但是当应用程序第一次打开时它不是。但在 iOS 13 中,它会在第一次打开时调用它。
我将 Xcode 从 12.2 更新到 12.3,问题仍然存在。有人可以检查他们是否收到 didBecomeActiveNotification使用 iOS 14 打开应用程序时调用?
知道如何进行这项工作吗?
附注。要在 Xcode 12 上使用 SceneDelegate 创建项目 AppName.app文件应该被删除,然后创建 SceneDelegate.swiftAppDelegate.swift带有样板代码的文件并编辑 info.plist “Application Scene Manifest”键使其使用SceneDelegate。

最佳答案

我做了一些测试,看来你是对的。我不确定这是错误还是有意的更改。
对我来说唯一可行的解​​决方案是 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)")
}
}
}
下面的其他测试(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)")
}
}
}

关于swiftui - UIApplication.didBecomeActiveNotification 未调用 iOS 14,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65618945/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com