在Info.plist中配置的场景时不使用AppDelegate

问题描述

我有一个旧的(Objective-C)iOS应用程序,我已将其扩展到Mac Catalyst,现在我想在不影响iOS版本的情况下向Mac版本添加多窗口支持。根据几本教程(example),我已经像这样设置了SceneDelegate ...

@implementation SceneDelegate

#if TARGET_OS_MACCATALYST

@synthesize window;

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    if ([scene isKindOfClass:[UIWindowScene class]]) {
        self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
        self.window.rootViewController = [[MainView alloc] init];
        [self.window makeKeyAndVisible];
    }
}

@end

#endif

...将此添加到AppDelegate ...

#if TARGET_OS_MACCATALYST

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

#endif

...并将其添加到Info.plist:

<key>UIApplicationSceneManifest</key>
<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <false/>
</dict>
<key>UIApplicationSceneManifest-macos</key>
<dict>
    <key>UIApplicationSupportsMultipleScenes</key>
    <true/>
    <key>UISceneConfigurations</key>
    <dict>
        <key>UIWindowSceneSessionRoleApplication</key>
        <array>
            <dict>
                <key>UISceneConfigurationName</key>
                <string>Main</string>
                <key>UISceneClassName</key>
                <string>UIWindowScene</string>
                <key>UISceneDelegateClassName</key>
                <string>SceneDelegate</string>
            </dict>
        </array>
    </dict>
</dict>

我仍然在AppDelegate的{​​{1}}方法中进行很多设置,但是从未调用过该方法,application:didFinishLaunchingWithOptions:也没有被调用,并且在安慰。因此,我将场景的定义从Info.plist文件移动到了application:configurationForConnectingSceneSession:options:方法中,就像这样……

application:configurationForConnectingSceneSession:options:

...仅将其保留在Info.plist中:

- (UISceneConfiguration *)application:(UIApplication *)application configurationForConnectingSceneSession:(nonnull UISceneSession *)connectingSceneSession options:(nonnull UISceneConnectionOptions *)options {
    UISceneConfiguration *config = [[UISceneConfiguration alloc] initWithName:@"Main" sessionRole:UIWindowSceneSessionRoleApplication];
    config.sceneClass = [UIWindowScene class];
    config.delegateClass = [SceneDelegate class];
    return config;
}

现在它可以正常工作了。

是否在Info.plist中定义场景配置以完全绕过<key>UIApplicationSceneManifest</key> <dict> <key>UIApplicationSupportsMultipleScenes</key> <false/> </dict> <key>UIApplicationSceneManifest-macos</key> <dict> <key>UIApplicationSupportsMultipleScenes</key> <true/> </dict> ?基于documentation,似乎应该绕过AppDelegate,但是我没有看到任何迹象表明它也应该绕过application:configurationForConnectingSceneSession:options:Some tutorials明确表示该方法仍应运行。

我尝试删除application:didFinishLaunchingWithOptions:方法,以防在两个地方定义配置使SDK感到困惑,但是只要application:configurationForConnectingSceneSession:options:在Info.plist中,就仍然没有调用application:didFinishLaunchingWithOptions:

我可以通过在第一行添加断点来判断是否调用UISceneConfigurations。除非我将application:didFinishLaunchingWithOptions:字典添加到Info.plist中,否则断点会被命中,但是仅进行更改会导致断点不被命中。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...