我想在 iOS 13 及更高版本上运行此代码我应该如何修复此错误?
我想让这段代码也可以在 iOS 13 上运行。
@available(iOS 14.0, *)
@main
struct WeatherProApp: App {
@Environment(\.scenePhase) private var scenePhase
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup{
let fetcher = WeatherFetcher()
let viewModel = WeeklyWeatherViewModel(weatherFethcer: fetcher)
WeeklyWeatherView(viewModel: viewModel)
}
.onChange(of: scenePhase) { (newScenePhase) in
switch newScenePhase {
case .active:
print("scene is now active!")
case .inactive:
print("scene is now inactive!")
case .background:
print("scene is now in the background!")
@unknown default:
print("Apple must have added something new!")
}
}
}
}
但它告诉我这个错误最佳答案
关注 @the.blaggy 答案,这是我设法在 iOS 13 上运行我的项目的方法:
SceneDelegate.swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let contentView = ContentView()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
}
Info.plist
<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>
WeatherProApp.swift
@main
struct WeatherProAppWrapper {
static func main() {
if #available(iOS 14.0, *) {
WeatherProApp.main()
}
else {
UIApplicationMain(CommandLine.argc, CommandLine.unsafeArgv, nil, NSStringFromClass(SceneDelegate.self))
}
}
}
关于swift - 在 Xcode 12 中使用 @main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62935053/