简体   繁体   English

Xcode 12 项目与 iOS 13 SwiftUI 支持:未调用 SceneDelegate 函数

[英]Xcode 12 project with iOS 13 SwiftUI support: SceneDelegate functions not invoked

I had created a project in Xcode 12 with iOS 14 as deployment target (bigger project).我在 Xcode 12 中创建了一个项目,其中 iOS 14 作为部署目标(更大的项目)。 After some time I realized that I had to also be able to support iOS 13 devices.一段时间后,我意识到我还必须能够支持 iOS 13 设备。 My project uses SwiftUI.我的项目使用 SwiftUI。

It is apparently not very straight-forward to simply change the deployment target to iOS 13 as some of the code generated by Xcode 12 is not backwards compatible with iOS 13.将部署目标简单地更改为 iOS 13 显然不是很直接,因为 Xcode 12 生成的一些代码不向后兼容 iOS 13。

So I essentially need to convert my Xcode 12-based project (currently with deployment target iOS 14.0) to also support iOS 13 .所以我基本上需要将我的基于 Xcode 12 的项目(当前部署目标为 iOS 14.0)转换为也支持 iOS 13

Here's what I did to prepare for my project's downgrade:以下是我为项目降级所做的准备工作:

  • Install Xcode 11.7, create new project with SwiftUI support and ensure the deployment target is iOS 13.0.安装 Xcode 11.7,创建支持 SwiftUI 的新项目,并确保部署目标为 iOS 13.0。 Thereby I know what Xcode believes the "iOS 13"-way should be, ie, using AppDelegate and SceneDelegate.因此,我知道 Xcode 认为“iOS 13”方式应该是什么,即使用 AppDelegate 和 SceneDelegate。
  • Run that app - works fine on my iOS 14 device.运行该应用程序 - 在我的 iOS 14 设备上运行良好。
  • Open Xcode 12, create new project with SwiftUI support, lower the deployment target to iOS 13.0 (because it's default set to 14.x), build it, and be presented with a bunch of compilation issues (as expected).打开 Xcode 12,创建支持 SwiftUI 的新项目,将部署目标降低到 iOS 13.0(因为它的默认设置为 14 个编译问题)(因为它的默认设置为 14 个)。

Changes then made to the Xcode 12 project:然后对 Xcode 12 项目进行更改:

  • Comment out everything in the <project>App.swift file.注释掉<project>App.swift文件中的所有内容。
  • Add AppDelegate.swift file with the same contents from the Xcode 11.7-generated project mentioned above.添加 AppDelegate.swift 文件,其内容与上述 Xcode 11.7 生成的项目中的内容相同。
  • Add SceneDelegate.swift file with the same contents from the Xcode 11.7-generated project mentioned above.添加与上述 Xcode 11.7 生成的项目中相同内容的 SceneDelegate.swift 文件。
  • Add a LaunchScreen.storyboard file (because Xcode warned about its non-existence).添加 LaunchScreen.storyboard 文件(因为 Xcode 警告它不存在)。
  • Run the app.运行应用程序。

The app runs, but the contents of ContentView is not shown.应用程序运行,但ContentView的内容未显示。 Just a black screen (black is probably because my device is in dark mode).只是一个黑屏(黑色可能是因为我的设备处于黑暗模式)。 Also, as you'll notice in the code below, I've inserted debug logs but only "didFinishLaunchingWithOptions" is printed in the console - that is, the scene delegate methods are not invoked, it seems.此外,正如您将在下面的代码中注意到的那样,我插入了调试日志,但控制台中只打印了“didFinishLaunchingWithOptions”——也就是说,似乎没有调用场景委托方法。

I've (of course) Googled a lot, tried various things with Info.plist, but nothing seems to help.我(当然)用 Google 搜索了很多,用 Info.plist 尝试了各种方法,但似乎没有任何帮助。

Code:代码:

AppDelegate.swift AppDelegate.swift

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        print("didFinishLaunchingWithOptions")
        return true
    }
    
    // MARK: UISceneSession Lifecycle
    
    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        print("configurationForConnecting")      
    }
    
    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        print("didDiscardSceneSessions")
    }
}

SceneDelegate.swift SceneDelegate.swift

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    var window: UIWindow?
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        print("scene willConnectTo")
        guard let windowScene = scene as? UIWindowScene else { return }
        let window = UIWindow(windowScene: windowScene)
        window.rootViewController = UIHostingController(rootView: ContentView())
        self.window = window
        window.makeKeyAndVisible()
    }
    
    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 ContentView.swift

struct ContentView: View {
    
    var body: some View {
        Text("Hello, world!")
            .foregroundColor(.blue)
            .padding()
            .onAppear {
                print("ContentView")
            }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Info.plist信息列表

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDisplayName</key>
    <string></string>
    <key>LSApplicationCategoryType</key>
    <string></string>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UIApplicationSceneManifest</key>
    <dict>
        <key>UIApplicationSupportsMultipleScenes</key>
        <false/>
        <key>UISceneConfigurations</key>
        <dict>
            <key>UIWindowSceneSessionRoleApplication</key>
            <array>
                <dict>
                    <key>UISceneConfigurationName</key>
                    <string>Default Configuration</string>
                    <key>UISceneDelegateClassName</key>
                    <string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
                </dict>
            </array>
        </dict>
    </dict>
    <key>UIApplicationSupportsIndirectInputEvents</key>
    <true/>
    <key>UILaunchScreen</key>
    <dict/>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

I'm going to guess that you forgot to add the scene delegate file to the app target.我猜您忘记将场景委托文件添加到应用程序目标。 When you build and run, look in the console and see if there's a complaint about not finding the scene delegate.构建和运行时,查看控制台,看看是否有关于找不到场景委托的抱怨。 That would explain the phenomenon.这可以解释这种现象。

If you've already built app using SwiftApp before implementation SceneDelegate , just uninstall and rebuild it.如果您在实现SceneDelegate之前已经使用 SwiftApp 构建了应用程序,只需卸载并重新构建它。 And then, you can resolve this problem.然后,您可以解决此问题。

persistentIdentifier will be generated by the system for creating new Scene when an app is launched at the first time.系统会在第一次启动应用程序时生成用于创建新Scene持久标识符。 After that, the identifier will be same one every app open.之后,每个应用程序打开的标识符都是相同的。 So the app doesn't need to create new Scene.所以应用程序不需要创建新场景。

That's why configurationForConnecting wasn't invoke but set up no rootview so it happens that your app shows black window.这就是为什么没有调用configurationForConnecting但没有设置 rootview 的原因,因此您的应用程序会显示黑色 window。

The following WWDC19 session video(around 31:40) says about the identifier.以下 WWDC19 session 视频(大约 31:40)介绍了标识符。
https://developer.apple.com/videos/play/wwdc2019/212/?time=1900 . https://developer.apple.com/videos/play/wwdc2019/212/?time=1900

Hope it will help your problem.希望对您的问题有所帮助。
Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM