Search code examples
objective-ccocoaicloudnsdocument

iCloud enabled - Stop the open file displaying on application launch?


I've just added iCloud support to an app that I am working on. Its working great, except that when I open the application without a document in focus the iCloud open file dialog appears and I don't want it to!

In my app delegate I have:

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

Which I use to show my own custom window. However now, both the iCloud open file dialog and my own dialog are displayed. Any ideas on how I can get rid of the iCloud dialog?

Default Window


Solution

  • I thought I would share my solution to this issue as I see others still looking for an answer. Its not a great solution but it does the trick.

    1. Subclass NSDocumentController and add the following:
    
    + (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:
    

    Add the following to your app delegate:

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

    And the reasoning -- By setting a breakpoint in openDocument I've found that its called before applicationDidFinishLaunching, applicationShouldOpenUntitledFile or applicationShouldHandleReopen:hasVisibleWindows: get called, meaning adding those methods is useless. Again, it's not great code but it works and does the trick. (None of the other solutions have worked for me).