簡體   English   中英

Xcode 12 項目與 iOS 13 SwiftUI 支持:未調用 SceneDelegate 函數

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

我在 Xcode 12 中創建了一個項目,其中 iOS 14 作為部署目標(更大的項目)。 一段時間后,我意識到我還必須能夠支持 iOS 13 設備。 我的項目使用 SwiftUI。

將部署目標簡單地更改為 iOS 13 顯然不是很直接,因為 Xcode 12 生成的一些代碼不向后兼容 iOS 13。

所以我基本上需要將我的基於 Xcode 12 的項目(當前部署目標為 iOS 14.0)轉換為也支持 iOS 13

以下是我為項目降級所做的准備工作:

  • 安裝 Xcode 11.7,創建支持 SwiftUI 的新項目,並確保部署目標為 iOS 13.0。 因此,我知道 Xcode 認為“iOS 13”方式應該是什么,即使用 AppDelegate 和 SceneDelegate。
  • 運行該應用程序 - 在我的 iOS 14 設備上運行良好。
  • 打開 Xcode 12,創建支持 SwiftUI 的新項目,將部署目標降低到 iOS 13.0(因為它的默認設置為 14 個編譯問題)(因為它的默認設置為 14 個)。

然后對 Xcode 12 項目進行更改:

  • 注釋掉<project>App.swift文件中的所有內容。
  • 添加 AppDelegate.swift 文件,其內容與上述 Xcode 11.7 生成的項目中的內容相同。
  • 添加與上述 Xcode 11.7 生成的項目中相同內容的 SceneDelegate.swift 文件。
  • 添加 LaunchScreen.storyboard 文件(因為 Xcode 警告它不存在)。
  • 運行應用程序。

應用程序運行,但ContentView的內容未顯示。 只是一個黑屏(黑色可能是因為我的設備處於黑暗模式)。 此外,正如您將在下面的代碼中注意到的那樣,我插入了調試日志,但控制台中只打印了“didFinishLaunchingWithOptions”——也就是說,似乎沒有調用場景委托方法。

我(當然)用 Google 搜索了很多,用 Info.plist 嘗試了各種方法,但似乎沒有任何幫助。

代碼:

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

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

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()
    }
}

信息列表

<?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>

我猜您忘記將場景委托文件添加到應用程序目標。 構建和運行時,查看控制台,看看是否有關於找不到場景委托的抱怨。 這可以解釋這種現象。

如果您在實現SceneDelegate之前已經使用 SwiftApp 構建了應用程序,只需卸載並重新構建它。 然后,您可以解決此問題。

系統會在第一次啟動應用程序時生成用於創建新Scene持久標識符。 之后,每個應用程序打開的標識符都是相同的。 所以應用程序不需要創建新場景。

這就是為什么沒有調用configurationForConnecting但沒有設置 rootview 的原因,因此您的應用程序會顯示黑色 window。

以下 WWDC19 session 視頻(大約 31:40)介紹了標識符。
https://developer.apple.com/videos/play/wwdc2019/212/?time=1900

希望對您的問題有所幫助。
謝謝你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM