`UIScene` lifecycle will soon be required. Failure to adopt will result in an assert in the future.

这是 Objective-C 版本的解决方案:

选项 1:更新到 UIScene 生命周期(推荐)

1. 更新 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>SceneDelegate</string>
                <key>UISceneStoryboardFile</key>
                <string>Main</string>
            </dict>
        </array>
    </dict>
</dict>

2. 创建 SceneDelegate.h 文件

#import <UIKit/UIKit.h>

@interface SceneDelegate : UIResponder <UIWindowSceneDelegate>

@property (strong, nonatomic) UIWindow * window;

@end

3. 创建 SceneDelegate.m 文件

 
#import "SceneDelegate.h"

@implementation SceneDelegate

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    UIWindowScene *windowScene = (UIWindowScene *)scene;
    self.window = [[UIWindow alloc] initWithWindowScene:windowScene];
    
    // 配置根视图控制器
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *initialViewController = [storyboard instantiateInitialViewController];
    self.window.rootViewController = initialViewController;
    [self.window makeKeyAndVisible];
}

@end

4. 更新 AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@end

5. 更新 AppDelegate.m

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 应用启动后的自定义代码
    return YES;
}

#pragma mark - UISceneSession lifecycle

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(UISceneSession *)connectingSceneSession options:(UISceneConnectionOptions *)options {
    return [[UISceneConfiguration alloc] initWithName:@"Default Configuration" sessionRole:connectingSceneSession.role];
}

@end

选项 2:禁用场景支持(如果不支持多窗口)

1. 更新 Info.plist

<key>UIApplicationSceneManifest</key>
<dict/>

2. 保持传统的 AppDelegate 设置

// AppDelegate.h
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

// AppDelegate.m
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 你的现有设置代码
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewController = [storyboard instantiateInitialViewController];
    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end

重要提示

  1. 删除旧的窗口属性:如果使用 Option 1,从 AppDelegate 中移除 window 属性

  2. 更新主入口:确保在 main.m 中仍然正确设置了 AppDelegate

  3. 清理项目:修改后执行 Product → Clean Build Folder

如果你的应用不需要支持 iPad 多窗口功能,Option 2 是更简单的解决方案。但如果要面向未来,建议采用 Option 1 的 UIScene 生命周期。

 

posted on 2025-10-29 16:41  高彰  阅读(21)  评论(0)    收藏  举报

导航