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

6

11

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

Woof answered 11/12, 2012 at 17:19 Comment(1)
Did anyone solve this problem? There are 3 questions in stack overflow about this, and none of the answers worked for me.Peafowl
W
0

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).

Woof answered 18/1, 2019 at 13:45 Comment(0)
I
4

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

NSDocument Support for iCloud

In 10.8, NSDocument-based applications with a ubiquity-container-identifiers entitlement gain new functionality and UI to facilitate iCloud document management.

When iCloud is enabled and an application is first launched or re-activated and no windows are visible or being restored, instead of creating a new Untitled document, NSDocumentController will display a non-modal open panel showing the user's iCloud library.

...

Applications that do not wish to use these features for any or all of their NSDocument subclasses can override +[NSDocument usesUbiquitousStorage] and return NO. If all of the application's declared NSDocument subclasses return NO from this method, then NSDocumentController will never show the new non-modal open panel.

So if you can give up using the features listed in this release note, return NO at +[NSDocument usesUbiquitousStorage]. I confirmed you can still open/save your file into iCloud storage from the normal dialog.

Indorse answered 27/1, 2017 at 22:51 Comment(2)
This doesn't work. usesUbiquitousStorage is a getter variable. You cannot override this method or set it. And if you disable this somehow it would disable iCloud. I need a way to bypass this screen with iCloud enabled.Peafowl
@Peafowl If you define your NSDocument class, you are of course able to override it. I could still use iCloud as I commented, but there might be hidden params to control this that I could not notice. I checked this on Yosemite to Sierra.Indorse
P
1

Putting below codes in your App Delegate lets you bypass that iCloud pop up New Document screen. Tested for 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];
    }
}
Peafowl answered 19/1, 2018 at 21:50 Comment(0)
C
0
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
    [mainWindowController.window makeKeyAndOrderFront:self];
    return NO;
}

This part is correct. I've just tested it.

Just make sure your that this class is really your app delegate.

  1. Make a new class called prefixAppDelegate
  2. In your MainMenu.xib, drag a new object to the side and set it's custom class to the app delegate class
  3. Right click Application and drag from Delegate down to your app delegate object.
  4. Now just paste the code above into your app delegate class

If that still doesn't help, try logging something in applicationShouldOpenUntitledFile:.

Also, I recommend not to set [mainWindowController.window makeKeyAndOrderFront:self]; in this method. You should rather use the app delegate method applicationDidFinishLaunching: method.

Cabbage answered 25/12, 2012 at 11:13 Comment(1)
applicationShouldOpenUntitledFile doesn't get called and instead that iCloud file window (photo in the original question) gets opened. If I disable iCloud in capabilities, everything work as expected. But I need iCloud functionality and bypass this file pop up screen.Peafowl
C
0

My observation and fix: [applicationShouldOpenUntitledFile:] won't be executed except you remove Key NSDocumentClass from *-info.plist. But this is harmful if your app is document based application, it won't open the document type you linked.

My fix is open my customised window directly in -(void)applicationWillFinishLaunching:(NSNotification *)notification method (Application delegate)

ETDocumentWindowController *windowController =  (ETDocumentWindowController*)get your own window controller here...;
[windowController.window makeKeyAndOrderFront:nil];
Crenellate answered 5/6, 2015 at 2:56 Comment(0)
W
0

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).

Woof answered 18/1, 2019 at 13:45 Comment(0)
A
-1

I ran into a similar problem -- it turned out that in my case, I had to remove the NSDocumentClass key and value from my Info.plist in the CFBundleDocumentTypes array. Only then would the applicationShouldOpenUntitledFile: method get called and thus allow me to prevent the iCloud/Document window from opening.

Alkylation answered 2/4, 2013 at 6:7 Comment(1)
Disabling this make Document-based app to stop working.Peafowl

© 2022 - 2024 — McMap. All rights reserved.