![](/img/trans.png)
[英]AppDelegate and SceneDelegate when supporting iOS 12 and 13
[英]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 12 項目進行更改:
<project>App.swift
文件中的所有內容。 應用程序運行,但ContentView
的內容未顯示。 只是一個黑屏(黑色可能是因為我的設備處於黑暗模式)。 此外,正如您將在下面的代碼中注意到的那樣,我插入了調試日志,但控制台中只打印了“didFinishLaunchingWithOptions”——也就是說,似乎沒有調用場景委托方法。
我(當然)用 Google 搜索了很多,用 Info.plist 嘗試了各種方法,但似乎沒有任何幫助。
代碼:
@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")
}
}
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")
}
}
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.