iOS 13 个应用在每次点击应用图标时请求一个新场景

iOS 13 app requests a new scene each time the app icon is tapped

我正在使用多个 windows 设置我的应用程序。它运作良好。但是当我从 springboard 打开我的应用程序时,它会创建一个新的 window every time.

我正在使用最新的 Xcode 和 iPadOS 13.0 测试版。我所有的视图控制器、视图等都是以编程方式制作的。我唯一的故事板是 LaunchScreen。

Info.plist

<key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <true/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default</string>
                    <key>UISceneDelegateClassName</key>
                    <string>ComicReader.SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>

AppDelegate.swift

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
    UISceneConfiguration(name: "Default", sessionRole: connectingSceneSession.role)
}

SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let scene = scene as? UIWindowScene else { return }

        window = UIWindow(windowScene: scene)
        window?.rootViewController = DelegateHelper.rootViewController()
        window?.makeKeyAndVisible()
    }
}

在 Apple 的 Gallery 示例中,如果打开应用程序,滑动到主屏幕,然后再次打开应用程序,我回到原来的位置,而无需再次调用 scene(_:willConnectTo)。在我自己的应用程序上,每次我打开应用程序时都会调用 scene(_:willConnectTo),并且放置断点显示我确实在每次启动时收到不同的 UIScene 和 UISceneSession 对象。

我没有显示任何 NSUserActivity 代码,因为我首先认为这是因为我还没有任何状态恢复。实施它不会改变任何事情。

如果你有一些想法,我很高兴读到你!

所以,我从上周开始就在找这个。今天,我决定评论我所有的AppDelegate、SceneDelegate,并且在Info.plist中只保留一个场景配置。从默认模板重写 AppDelegate & SceneDelegate 以逐步挖掘。

它在第一次尝试使用默认模板时有效。我重写了所有相同的东西......剧照有效。

问题? UIWindowSceneSessionRoleApplication 数组中 Info.plist 中的 "Default" 配置是 "Item 1",而不是 "Item 0"。 git 把所有东西都藏起来,只重新排序使它也能正常工作。

我希望这对某人有所帮助。