【问题标题】:can't create an app programmatically for older iOS versions on xCode 12无法在 xCode 12 上以编程方式为较旧的 iOS 版本创建应用程序
【发布时间】:2021-11-26 16:04:36
【问题描述】:

我无法在 Xcode 12 上以编程方式为较旧的 iOS 版本创建应用程序。 AppDelegate.swift 中有我的代码

    @main
    class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        // Set the window bounds
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()

        window?.rootViewController = UINavigationController(rootViewController: ViewController())
        
        return true
    }
  }

这是我的 ViewController:

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
    }


}

我在互联网上寻找解决方案,但找不到。 我删除了 Main.storyboard 和 SceneDelegate.swift 文件。我删除 info.plist 文件中的键/值 main 。我将 Main.storyboard 删除到主界面中

【问题讨论】:

  • 什么问题?

标签: ios swift xcode version


【解决方案1】:

不要删除 sceneDelegate 文件,这是一种不好的做法,appDelegate 将很快弃用... 如果您需要为使用 iOS 12 或更早版本的设备进行设置.. 尝试使用这个..

在场景委托中...

  func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let scene = (scene as? UIWindowScene) else { return }

        let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue

        if floatVersion >= 13 {
            window = UIWindow(windowScene: scene)
            window?.makeKeyAndVisible()
            
            let controller = YourController()
            let navigation = UINavigationController(rootViewController: controller)

            window?.rootViewController = navigation
        }
    }

并在 appDelegate 中使用它。仅适用于在 appDelegate 文件中使用 iOS 12 或更早版本的设备


var window: UIWindow?

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let floatVersion = (UIDevice.current.systemVersion as NSString).floatValue
        if floatVersion <= 12.5 {
            window = UIWindow(frame: UIScreen.main.bounds)
            window?.makeKeyAndVisible()
            
// if you use a navigation... 
            let controller = YourController()
            let navigation = UINavigationController(rootViewController: controller)
            window?.rootViewController = navigation
        } 
      
        return true
    }
 

尝试在验证中使用场景委托和 appDelegate 中的断点,使用 iOS 12 版本和 iOS 13 版本或最新版本的模拟器......并且会工作,但不要删除场景委托,查看 wwdc swift 新闻。

我在 2 年前遇到了同样的问题,我目前使用这个选项并且仍然可以为我工作,我希望这会有所帮助。

【讨论】:

  • 感谢您的回答,但问题已通过其他方式解决。
猜你喜欢
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 2018-11-20
  • 2013-03-26
  • 2022-01-18
  • 2018-12-14
相关资源
最近更新 更多