如何将Swift项目的iOS Deployment Target从8.0升级为支持iOS10-11.3?
如何将Swift项目改为仅支持iOS 10.0至11.3版本
嘿,我来帮你搞定这个问题!你提到的只修改Deployment Target是不够的——它只能设置最低支持版本,没法限制最高版本。下面是完整的操作步骤:
一、设置最低支持版本(iOS 10.0)
这一步就是你图里操作的修改Deployment Target,具体步骤:
- 打开Xcode,选中左侧导航栏最顶部的项目根图标
- 切换到右侧的「Info」标签页,找到「Deployment Target」区域,把「iOS Deployment Target」改成iOS 10.0
- 或者去「Build Settings」标签页,搜索「iOS Deployment Target」,直接设置为10.0
完成这一步后,你的App就无法在iOS 10.0以下的设备上安装或运行了,但还没限制iOS 11.3以上的设备,所以还需要下面的操作。
二、限制最高支持版本(iOS 11.3)
Xcode没有直接的可视化开关来设置最高支持版本,我们需要通过代码检查+Info.plist配置来实现:
1. 启动时强制版本检查(核心步骤)
在AppDelegate.swift的启动方法里添加版本校验逻辑,一旦检测到系统版本高于11.3就弹出提示并退出App:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { let currentSystemVersion = UIDevice.current.systemVersion // 把版本号拆分成整数数组,方便比较 guard let majorVersion = Int(currentSystemVersion.components(separatedBy: ".").first ?? ""), let minorVersion = Int(currentSystemVersion.components(separatedBy: ".")[1] ?? "") else { return true } // 检查是否超过iOS 11.3 if majorVersion > 11 || (majorVersion == 11 && minorVersion > 3) { let alert = UIAlertController(title: "系统版本不支持", message: "本应用仅支持iOS 10.0至11.3,请使用符合要求的设备。", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "确定", style: .default, handler: { _ in exit(0) })) // 确保能弹出提示 if let rootVC = UIApplication.shared.windows.first?.rootViewController { rootVC.present(alert, animated: true) } } return true }
2. Info.plist辅助配置(可选)
为了让App Store和系统更好地识别你的版本范围,可以在Info.plist里添加两个键:
- 右键Info.plist → 「Add Row」,添加
LSMinimumSystemVersion,类型选String,值填10.0 - 再添加
LSMaximumSystemVersion,类型String,值填11.3
注意:这个配置主要是展示用的,不会强制限制运行,所以必须配合上面的代码检查才能生效。
三、对你疑问的解答
仅修改Deployment Target不能满足你的需求。因为iOS Deployment Target的作用只是定义App能运行的最低系统版本,它会阻止低版本设备安装,但不会拦截高版本设备。要实现「仅支持10.0-11.3」,必须同时设置最低版本+代码限制最高版本。
最后提醒一下:记得用iOS 10.0、11.3以及更高版本的设备/模拟器测试,确保版本校验逻辑正常,同时检查项目中用到的API在10.0-11.3之间都是可用的,避免出现兼容性崩溃。
内容的提问来源于stack exchange,提问作者Nazar
相关产品推荐
相关产品推荐

