前言:为什么要适配 UIScene?
从 iOS 26 开始。Apple 进一步强化了以 UIScene 为核心的应用生命周期模型,并正式将传统的 AppDelegate 中与 UI 相关的职责完全移交给 SceneDelegate。
不适配会怎么样?
如果你的项目还是依赖 AppDelegate 管理窗口和 UI 生命周期:那么在 iOS 26 之后的下一个主要版本中,在使用最新 SDK 构建应用时,将强制要求使用 UIScene 生命周期,否则应用将无法启动。
TN3187: Migrating to the UIKit scene-based life cycle
“In the next major release following iOS 26, UIScene lifecycle will be required when building with the latest SDK; otherwise, your app won’t launch.
此外,自 2026 年 4 月 28 日起,上传到 App Store Connect 的 App 必须使用 iOS 26 和 iPadOS 26 SDK 或更高版本构建。
UIScene 适配已经迫在眉睫了 🔥🔥🔥
一、核心概念:先搞清楚几个对象
在适配之前,我们需要理解 UIScene 中的几个核心对象的关系:
| |
|---|
| |
| |
| |
| 管理一个“窗口场景”(一块屏幕的内容),每个 Session 对应一个 Scene |
| |
| |
二、完整迁移步骤
第一步:配置 Info.plist
在 Info.plist 中增加 Scene 配置,告诉系统你的 App 需要使用 scene-based 生命周期:
<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> <key>UISceneStoryboardFile</key> <string>Main</string> </dict> </array> </dict></dict>
也可以不在 Info.plist 中配置,而是通过代码动态返回配置,这种方式更加灵活:
// swift @available(iOS 13.0, *)func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { var sceneConfiguration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) sceneConfiguration.delegateClass = SceneDelegate.self // 如果使用 storyboard 创建根控制器,则需要指定对应的故事板 sceneConfiguration.storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main) return sceneConfiguration}// oc- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options API_AVAILABLE(ios(13.0)) { UISceneConfiguration *sceneConfiguration = [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role]; sceneConfiguration.delegateClass = SceneDelegate.class; sceneConfiguration.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:NSBundle.mainBundle]; return sceneConfiguration;}
第二步:创建 SceneDelegate
创建 SceneDelegate 文件
- OC:SceneDelegate.h、SceneDelegate.m
- Swift: SceneDelegate.swift
#import <UIKit/UIKit.h>API_AVAILABLE(ios(13.0))@interfaceSceneDelegate : UIResponder <UIWindowSceneDelegate>@property (strong, nonatomic) UIWindow * window;@end@implementationSceneDelegate- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { UIWindowScene *wSence = (UIWindowScene *)scene; if (![wSence isKindOfClass:UIWindowScene.class]) { return; } self.window = [[UIWindow alloc] initWithWindowScene:wSence]; self.window.backgroundColor = UIColor.whiteColor; self.window.rootViewController = [YourRootViewController new]; [self.window makeKeyAndVisible]; // 处理通过 URL / Universal Link / Shortcut 冷启动场景 [self handleConnectionOptions: connectionOptions]}@end
注意:
如果你的项目还需要支持 iOS 13 以下的系统,注意在类或者方法上加上系统版本可用性声明,告诉编译器和运行时:这段代码(方法/类型/调用点)只允许在 iOS 13 及以上使用,避免低版本崩溃和编译警告。
- Swift:
@available(iOS 13.0, *) - OC:
API_AVAILABLE(ios(13.0))
第三步:迁移 AppDelegate 代码
迁移的核心原则:AppDelegate 只保留进程级别的职责
- UI 初始化代码迁移至 SceneDelegate。例如 UIWindow 创建
- 应用前后台和状态切换方法迁移至 SceneDelegate
@interfaceAppDelegate : UIResponder <UIApplicationDelegate>// 失效,请勿继续使用@property (strong, nonatomic) UIWindow * window;@end@implementationAppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { if (@available(iOS 13.0, *)) { // iOS 13+ window 由 SceneDelegate 创建 // UI 初始化迁移至 - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions } else { self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *root = [ViewController new]; // Your ViewController self.window.rootViewController = root; [self.window makeKeyAndVisible]; } return YES;}- (void)applicationDidBecomeActive:(UIApplication *)application { // 迁移至 - (void)sceneDidBecomeActive:(UIScene *)scene}- (void)applicationWillResignActive:(UIApplication *)application { // 迁移至 - (void)sceneWillResignActive:(UIScene *)scene}- (void)applicationWillEnterForeground:(UIApplication *)application { // 迁移至 - (void)sceneWillEnterForeground:(UIScene *)scene}- (void)applicationDidEnterBackground:(UIApplication *)application { // 迁移至 - (void)sceneDidEnterBackground:(UIScene *)scene}@end
第四步:处理 URL / Deep Link / Shortcut
在 Scene 架构下,我们还需要处理 URL / Deep Link / Shortcut 的相关代码。包含冷热启动两种场景
冷启动:通过 connectionOptions 获取
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions { UIOpenURLContext *URLContext = connectionOptions.URLContexts.anyObject; if (URLContext) { // 处理 URLContext.URL } NSUserActivity *userActivity = connectionOptions.userActivities.anyObject; if (userActivity.activityType == NSUserActivityTypeBrowsingWeb) { // 处理 Universal Link } UIApplicationShortcutItem *shortcutItem = connectionOptions.shortcutItem; if (shortcutItem) { // 处理 Shortcut }}
热启动:通过独立回调获取
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts { // 处理 URLContext.URL}- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity { // 处理 Universal Link}- (void)windowScene:(UIWindowScene *)windowScene performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler { // 处理 Shortcut}
常见问题与踩坑总结
踩坑 1: 启动后黑屏
- 没有创建 UIWindowSceneDelegate 代理
- Delegate Class Name 没有设置或者类名错误
- 根控制器(root view controller)是从 storyboard 加载,Storyboard Name 没有设置或者名字错误
Xcode 日志:
There is no scene delegate set. A scene delegate class must be specified to use a main storyboard file.
踩坑 2: AppDelegate 中的 window 属性不再生效
- 现象:在 AppDelegate 设置了 window ,但 UI 界面没有任何变化
- 原因:在 Scene 架构中,窗口由 SceneDelegate 管理,AppDelegate.window 不再与实际的显示窗口关联
注意:
使用 UIScene 后,iOS 13+ [UIApplication sharedApplication].delegate.window 方法调用正常会返回 nil,我们不能依赖这个接口来获取 App delegate 的 window。需要通过默认 UIWindowScene 来获取对应的 window,代码如下:
static NSString * const kDefaultSceneConfigurationName = @"Default Configuration";@implementation UIApplication(YourPrefix)- (nullable UIWindow *)yourPrefix_window { UIWindow *window = nil; if (@available(iOS 13.0, *)) { UIWindowScene *dwScene = [self yourPrefix_defaultScene]; id<UIWindowSceneDelegate> wDelegate = (id<UIWindowSceneDelegate>)dwScene.delegate; if ([wDelegate respondsToSelector:@selector(window)]) { window = wDelegate.window; } } else { window = self.delegate.window; } return window;}- (nullable UIWindowScene *)yourPrefix_defaultScene API_AVAILABLE(ios(13.0)) { NSAssert(NSThread.isMainThread, @"Must be called on main thread"); UIWindowScene *dwScene = nil; UIWindowScene *activeScene = nil; if (!self.supportsMultipleScenes) { UIScene *scene = self.connectedScenes.anyObject; if ([scene isKindOfClass:UIWindowScene.class]) { dwScene = (UIWindowScene *)scene; } } else { for (UIScene *scene in self.connectedScenes) { if (![scene isKindOfClass:UIWindowScene.class]) { continue; } if (scene.activationState != UISceneActivationStateForegroundActive) { continue; } UIWindowScene *ws = (UIWindowScene *)scene; if (!activeScene) { activeScene = ws; } if ([ws.session.configuration.name isEqualToString:kDefaultSceneConfigurationName]) { dwScene = ws; break; } } if (!dwScene) { dwScene = activeScene; } } return dwScene;}@end
踩坑 3: 自定义 UIWindow 无法显示
- 现象:适配 UIScene 后,发现自定义的 Window 无法展示
- 原因:window 创建后必须给它指定
UIWindowScene,否则无法展示 - 解决:设置 UIWindowScene 有以下两种方式:
1、初始化指定 UIWindowScene
- (instancetype)initWithWindowScene:(UIWindowScene *)windowScene;
2、初始化不指定 UIWindowScene,通过属性 windowScene 进行赋值。注意:更新 UIWindowScene 比较耗性能!!!
// 如果设置为 nil,窗口就不会显示在任何屏幕上// 切换 UIWindowScene 可能是一个比较耗性能的操作,所以尽量别在对性能要求比较高的代码里去做这个事情。@property(nullable, nonatomic, weak) UIWindowScene *windowScene API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(watchos);
踩坑 4: 多窗口下的单例状态冲突
- 现象:iPad 多窗口模式下,两个窗口共享一个单例,导致状态混乱
- 解决:单例注意考虑使用 Scene 级别的状态管理
迁移检查清单
基础配置
- Info.plist 包含 UIApplicationSceneManifest
- AppDelegate 中移除 window 相关逻辑
- AppDelegate 中移除 UI 生命周期回调
- UIWindow 创建使用
initWithWindowScene 而非 initWithFrame
生命周期
- sceneDidBecomeActive 代替 applicationDidBecomeActive
- sceneWillResignActive 代替 applicationWillResignActive
- sceneDidEnterBackground 代替 applicationDidEnterBackground
- sceneWillEnterForeground 代替 applicationWillEnterForeground
Deep Link / URL
- 冷启动:connectionOptions.URLContexts.anyObject
- 热启动:- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
- 冷启动:connectionOptions.userActivities.anyObject
- 热启动:- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity
Quick Actions
- 冷启动:connectionOptions.shortcutItem
- 热启动:- (void)windowScene:(UIWindowScene *)windowScene performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
推送通知
- 冷启动:connectionOptions.notificationResponse
API 替换
- 使用 connectedScenes 替代 UIApplication.sharedApplication.keyWindow
- 使用 UIWindowScene.screen 替代 UIScreen.mainScreen
- UIWindow 创建使用
initWithWindowScene 替代 initWithFrame
多窗口(如支持)
- UIApplicationSupportsMultipleScenes 设置为 true
- stateRestorationActivity 已实现
测试场景
- 冷热启动 Deep Link / URL / Quick Actions
参考
- 【即将生效的 SDK 最低要求】https://developer.apple.com/cn/news/?id=ueeok6yw
- 【TN3187: Migrating to the UIKit scene-based life cycle】https://developer.apple.com/documentation/technotes/tn3187-migrating-to-the-uikit-scene-based-life-cycle
- 【Specifying the scenes your app supports】https://developer.apple.com/documentation/UIKit/specifying-the-scenes-your-app-supports
- 【Managing your app’s life cycle】:https://developer.apple.com/documentation/UIKit/managing-your-app-s-life-cycle
- 【UIWindowScene】:https://developer.apple.com/documentation/UIKit/UIWindowScene
“写在最后: 如果这篇文章对你有帮助,欢迎点赞、收藏、转发。如有任何问题或踩坑经历,欢迎在评论区交流 🍻