在Xocde12上开发面向低于iOS13的Swift应用
1、创建项目
在Xocde12上我们创建swift项目的时候Interface有两种选择 SwiftUI 和 Storyboard。其中SwiftUI只支持iOS13以上的系统 所以如果需要面向iOS13以下系统则无法使用。那么只能使用Storyboard LifeCycle使用UIKit App Delegate。但是Xocde12默认为我们创建的项目如下
这时候我们无法在iOS13以下系统上运行 会提示iOS Deployment Target过高。
查看在build setting中的iOS Deployment Target 发现默认是iOS14.x 我们将它改到iOS13以下系统 如12.3 。
降级iOS Deployment Target后再编译后发现项目报了大量的错误 错误类似
ConnectionOptions is only available in iOS 13.0 or newer
UIScene is only available in iOS 13.0 or newer
UISceneConfiguration is only available in iOS 13.0 or newer
UISceneSession is only available in iOS 13.0 or newer
UIWindowScene is only available in iOS 13.0 or newer
...
这是因为默认创建的项目中在AppDelegate和SceneDelegate中使用了UIScene等只有iOS13以上系统才支持的功能。
解决方法是添加 available(iOS 13.0, *) 标识 比如AppDelegate
import UIKit main class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) - Bool { // Override point for customization after application launch. return true // MARK: UISceneSession Lifecycle available(iOS 13.0, *) func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) - UISceneConfiguration { // Called when a new scene session is being created. // Use this method to select a configuration to create the new scene with. return UISceneConfiguration(name: Default Configuration , sessionRole: connectingSceneSession.role) available(iOS 13.0, *) func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set UISceneSession ) { // Called when the user discards a scene session. // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 复制代码
为后两个函数添加这个标识 这样只有在iOS13.0以上系统才会加载。而SceneDelegate则需要为整个类添加这个标识
import UIKit available(iOS 13.0, *) class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? 复制代码
这样再编译就不会报错了 在iOS 13.0以上系统依然可以正常运行。
解决上面的问题后 在iOS13以下系统上可以运行了 但是运行后发现黑屏。这是因为AppDelegate将管理整个App生命周期的任务委托给了SceneDelegate 所以window属性移到了SceneDelegate下 如下
import UIKit available(iOS 13.0, *) class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? 复制代码
但是SceneDelegate在iOS13以下系统并不加载 所以没有window属性自然就黑屏了。解决方法就是在AppDelegate中也添加一个window属性 如下
import UIKit main class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) - Bool { // Override point for customization after application launch. return true // MARK: UISceneSession Lifecycle 复制代码
这样再运行就可以看到正常页面了。
这样之后
iOS13以下版本的时候 window就走AppDelegate这里 不会黑屏
iOS13或以上版本的时候 window就走SceneDelegate 被委托人 这里 不会黑屏
用 Swift 开发我的第一个 iOS 应用前,我想要知道这些内容 本文讲的是用 Swift 开发我的第一个 iOS 应用前,我想要知道这些内容,Swift 是一款由苹果公司(世界最大的公司之一)创立的编程语言。这也表明了许多事:首先,苹果并不避讳向自己的平台中引入重大的更新。你需要知道的是,这里的重大更新,我指的是包括优先级更新在内的一系列更新措施。
[译]iOS 开发中使用 Swift 进行 iBeacons 交互指南 本文讲的是[译]iOS 开发中使用 Swift 进行 iBeacons 交互指南,我最近致力于研究一个关于 iBeacons 的 iOS 项目。本文中,我将全面的介绍如何使用 Swift 进行 iOS 项目中 iBeacons 开发。
相关文章
- 伪装word应用的安卓木马分析报告
- 《SAP HANA平台应用开发》—第3章3.3节分析视图
- 《 线性代数及其应用 (原书第4版)》——1.6 线性方程组的应用
- 全网最全Android compose开发应用指南
- 大数据应用开发线上班(学徒班)课程大纲
- 《Android应用开发入门经典(第3版)》——第1.5节定制应用
- 《Android 应用案例开发大全(第3版)》——第1.5节本书案例项目的导入
- iOS~判断应用是否有定位权限
- Android应用Design Support Library完全使用实例
- 5款应用优化Outlook的体验感
- Qt编写地图综合应用18-地图模式
- __attribute__详解及应用
- Flink CDC 在易车的应用实践
- 《Swift iOS应用开发实战》——第2章 搭建计算器的界面
- 《Swift iOS应用开发实战》——2.2 了解故事板
- 《Swift iOS应用开发实战》——3.4了解视图控制器
- 保护ASP.NET 应用免受 CSRF 攻击
- 【Unity3D应用案例系列】计算器工具开发
- OpenHarmony应用开发【01】
- 安卓APP应用在各大应用市场上架方法整理
- Android应用开发-小巫CSDN博客client之获取评论列表
- IBM将Swift带到云端,彻底简化端到端应用开发
- 玩转spring boot——MVC应用