【问题标题】:iCloud enabled - Stop the open file displaying on application launch?启用 iCloud - 在应用程序启动时停止显示打开的文件?
【发布时间】:2012-11-29 07:11:11
【问题描述】:

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

在我的应用委托中,我有:

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

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

【问题讨论】:

  • 有人解决了这个问题吗?堆栈溢出中有 3 个关于此的问题,但没有一个答案对我有用。

标签: objective-c cocoa icloud nsdocument


【解决方案1】:

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

对 iCloud 的 NSDocument 支持

在 10.8 中,具有 ubiquity-container-identifiers 权利的基于 NSDocument 的应用程序获得了新的功能和 UI,以促进 iCloud 文档管理。

当启用 iCloud 并且首次启动或重新激活应用程序并且没有可见或正在恢复的窗口时,NSDocumentController 将显示一个显示用户 iCloud 库的非模态打开面板,而不是创建一个新的无标题文档。

...

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

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

【讨论】:

  • 这不起作用。 usesUbiquitousStorage 是一个 getter 变量。您不能覆盖或设置此方法。如果你以某种方式禁用它,它将禁用 iCloud。我需要一种在启用 iCloud 的情况下绕过此屏幕的方法。
  • @coolcool1994 如果你定义你的 NSDocument 类,你当然可以覆盖它。正如我评论的那样,我仍然可以使用 iCloud,但可能有隐藏的参数来控制我无法注意到的。我在优胜美地到塞拉利昂检查了这个。
【解决方案2】:

在您的 App Delegate 中放置以下代码可让您绕过 iCloud 弹出的“新建文档”屏幕。经过高山脉测试。

-(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];
    }
}

【讨论】:

    【解决方案3】:
    - (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: 方法。

    【讨论】:

    • applicationShouldOpenUntitledFile 没有被调用,而是打开了 iCloud 文件窗口(原始问题中的照片)。如果我在功能中禁用 iCloud,一切都会按预期工作。但我需要 iCloud 功能并绕过此文件弹出屏幕。
    【解决方案4】:

    我的观察和修正: [applicationShouldOpenUntitledFile:] 不会被执行,除非你从 *-info.plist 中删除 Key NSDocumentClass。但是,如果您的应用程序是基于文档的应用程序,这是有害的,它不会打开您链接的文档类型。

    我的解决方法是直接在-(void)applicationWillFinishLaunching:(NSNotification *)notification 方法(应用程序委托)中打开我的自定义窗口

    ETDocumentWindowController *windowController =  (ETDocumentWindowController*)get your own window controller here...;
    [windowController.window makeKeyAndOrderFront:nil];
    

    【讨论】:

      【解决方案5】:

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

      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: 被调用之前被调用,这意味着添加这些方法是无用的。同样,这不是很好的代码,但它可以工作并且可以解决问题。 (其他解决方案都不适合我)。

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 禁用这个使基于文档的应用程序停止工作。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-22
        • 1970-01-01
        相关资源
        最近更新 更多