防止基于NSDocument的应用程序在崩溃后重新打开文档

问题描述 投票:0回答:1

我有一个用于macOS的只读音乐播放器应用程序,它使用NSDocument免费获取所有文件处理逻辑。

我现在遇到的问题是,当一个或多个播放器窗口打开时,每次应用程序崩溃(或被调试器停止)时,它们会在应用程序重新启动时自动重新打开。我不希望这样,因为它会干扰调试,并且这个应用程序不会发生合法的崩溃。

Apple的NSDocument文档中没有关于重新打开文件的内容,所以我在那里运气不好。有没有正确的方法来做到这一点?

swift cocoa nsdocument
1个回答
1
投票

首先创建NSDocumentController的子类,并确保在- (void)applicationWillFinishLaunching:(NSNotification *)notification中创建它的实例,以便它成为sharedDocumentController。 (见this question

然后在您的子类中覆盖还原方法:

+ (void)restoreWindowWithIdentifier:(NSString *)identifier state:(NSCoder *)state completionHandler:(void (^)(NSWindow *, NSError *))completionHandler
{
    if (_preventDocumentRestoration) { // you need to decide when this var is true
        completionHandler(nil, [NSError errorWithDomain:NSCocoaErrorDomain code:NSUserCancelledError userInfo:nil]);
    }
    else {
        [super restoreWindowWithIdentifier:identifier state:state completionHandler:completionHandler];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.