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

11

我刚刚为我正在开发的应用程序添加了iCloud支持,它工作得很好,但是当我打开应用程序时,如果没有文档在焦点中,iCloud打开文件对话框会出现,而我不希望它出现!

在我的应用程序代理中,我有:

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

我使用它来展示我的自定义窗口。然而现在,既出现了iCloud打开文件对话框,也出现了我的对话框。你有什么想法如何去除iCloud对话框吗?

默认窗口


1
有人解决了这个问题吗?在Stack Overflow上有3个关于这个问题的提问,但是没有一个答案对我有用。 - coolcool1994
6个回答

4

https://developer.apple.com/library/prerelease/content/releasenotes/AppKit/RN-AppKitOlderNotes/index.html

iCloud的NSDocument支持

在10.8版本中,具有普及容器标识符权限的基于NSDocument的应用程序获得了新功能和UI以便于iCloud文档管理。

当启用iCloud且首次启动或重新激活应用程序时,如果没有可见窗口或正在恢复的窗口,NSDocumentController将显示一个非模态打开面板,显示用户的iCloud库。

...

不希望为其任何或所有NSDocument子类使用这些功能的应用程序可以覆盖+ [NSDocument usesUbiquitousStorage]并返回NO。如果应用程序的所有声明的NSDocument子类都从此方法返回NO,则NSDocumentController永远不会显示新的非模态打开面板。

因此,如果您可以放弃本发布说明中列出的功能,请在+[NSDocument usesUbiquitousStorage]处返回NO。 我确认您仍然可以从正常对话框中打开/保存文件到iCloud存储中。


1
这个不起作用。usesUbiquitousStorage是一个getter变量。你不能重写这个方法或设置它。如果你以某种方式禁用它,它会禁用iCloud。我需要一种方法来绕过这个屏幕并启用iCloud。 - coolcool1994
@coolcool1994 如果你定义了NSDocument类,当然可以重写它。我仍然可以像我评论的那样使用iCloud,但可能有一些隐藏的参数来控制这个,我可能没有注意到。我在Yosemite到Sierra上检查过这个。 - ioue

1
将以下代码放入您的应用程序委托中,可以让您绕过iCloud弹出的新文档屏幕。经过High Sierra测试。
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
    // Schedule "Checking whether document exists." into next UI Loop.
    // Because document is not restored yet. 
    // So we don't know what do we have to create new one.
    // Opened document can be identified here. (double click document file)
    NSInvocationOperation* op = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(openNewDocumentIfNeeded) object:nil];
    [[NSOperationQueue mainQueue] addOperation: op];
}

-(void)openNewDocumentIfNeeded
{
    NSUInteger documentCount = [[[NSDocumentController sharedDocumentController] documents]count];

    // Open an untitled document what if there is no document. (restored, opened).       
    if(documentCount == 0){
        [[NSDocumentController sharedDocumentController]openUntitledDocumentAndDisplay:YES error: nil];
    }
}

0
我的观察和修复: 只有在从 *-info.plist 中删除 Key NSDocumentClass 时,[applicationShouldOpenUntitledFile:] 才会执行。但是,如果您的应用程序是文档为基础的应用程序,则这是有害的,它不会打开您链接的文档类型。
我的修复方法是直接在-(void)applicationWillFinishLaunching:(NSNotification *)notification方法中打开我的自定义窗口(应用程序代表)。
ETDocumentWindowController *windowController =  (ETDocumentWindowController*)get your own window controller here...;
[windowController.window makeKeyAndOrderFront:nil];

0
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    [mainWindowController.window makeKeyAndOrderFront:self];
    return NO;
}

这部分是正确的。我刚测试过了。

只要确保这个类真的是你的应用程序委托。

  1. 创建一个名为prefixAppDelegate的新类
  2. 在你的MainMenu.xib中,将一个新对象拖到侧边,并将其自定义类设置为应用程序委托类
  3. 右键单击Application,从Delegate向下拖动到你的应用程序委托对象。
  4. 现在只需将上面的代码粘贴到你的应用程序委托类中即可

如果仍然无法解决问题,请尝试在applicationShouldOpenUntitledFile:中记录一些内容。

此外,我建议不要在这个方法中设置[mainWindowController.window makeKeyAndOrderFront:self];。你应该使用应用程序委托方法applicationDidFinishLaunching:方法。


1
applicationShouldOpenUntitledFile不会被调用,相反会打开iCloud文件窗口(原始问题中的照片)。如果我在功能设置中禁用iCloud,则一切都按预期工作。但我需要iCloud功能并绕过此文件弹出屏幕。 - coolcool1994

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:

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

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

    ...
}

而且推理是这样的——通过在openDocument中设置断点,我发现它在applicationDidFinishLaunchingapplicationShouldOpenUntitledFileapplicationShouldHandleReopen:hasVisibleWindows:被调用之前就已经被调用了,这意味着添加这些方法是无用的。再次强调,这不是很好的代码,但它能够工作并完成任务。(其他解决方案都没有对我起作用)。


-1
我遇到了类似的问题 - 结果发现在我的情况下,我必须从我的Info.plist文件中的CFBundleDocumentTypes数组中删除NSDocumentClass键和值。只有这样,applicationShouldOpenUntitledFile:方法才会被调用,从而允许我防止iCloud /文档窗口打开。

禁用此功能将导致基于文档的应用程序停止工作。 - coolcool1994

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接