启用iCloud - 停止在应用程序启动时显示的打开文件?

Kyl*_*yle 11 cocoa objective-c nsdocument icloud

我刚刚将iCloud支持添加到我正在处理的应用程序中.它工作得很好,除了当我打开没有文档焦点的应用程序时,iCloud打开文件对话框出现了,我不想要它!

在我的应用代表中,我有:

- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    [mainWindowController.window makeKeyAndOrderFront:self];
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

我用它来显示我自己的自定义窗口.但是现在,将显示iCloud打开文件对话框和我自己的对话框.关于如何摆脱iCloud对话框的任何想法?

默认窗口

Kyl*_*yle 0

我想我会分享我对这个问题的解决方案,因为我看到其他人仍在寻找答案。这不是一个很好的解决方案,但它确实有效。

  1. 子类 NSDocumentController 并添加以下内容:

+ (void) setCanOpenUntitledDocument: (BOOL) _canOpenUntitledDocument
{
    canOpenUntitledDocument = _canOpenUntitledDocument;
} // End of setCanOpenUntitledDocument:

- (void) openDocument: (id) sender
{
    // With iCloud enabled, the app keeps trying to run openDocument: on first launch (before apphasfinishedlaunching gets set.
    // This method lets us check and see if the app has finished launching or not. If we try to open a document before
    // its finished, then don't let it.
    if(!canOpenUntitledDocument)
    {
        return;
    } // End of appHasFinishedLaunching not set

    [super openDocument: sender];
} // End of openDocument:
Run Code Online (Sandbox Code Playgroud)

将以下内容添加到您的应用程序委托中:


- (void) applicationDidFinishLaunching: (NSNotification *) aNotification
{
    // Finished launching. Let us open untitled documents.
    [SQLProDocumentController setCanOpenUntitledDocument: true];

    ...
}
Run Code Online (Sandbox Code Playgroud)

推理 - 通过在中设置断点,openDocument我发现它在之前被调用applicationDidFinishLaunchingapplicationShouldOpenUntitledFile或者applicationShouldHandleReopen:hasVisibleWindows:被调用,这意味着添加这些方法是无用的。再说一次,这不是很好的代码,但它可以工作并且可以达到目的。(其他解决方案都不适合我)。