Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9038173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:25:29+00:00 2026-06-16T09:25:29+00:00

I have an application based on NSDocument with an NSDocumentController subclass. My NSDocument works

  • 0

I have an application based on NSDocument with an NSDocumentController subclass. My NSDocument works with both file URLs and URLs with a custom scheme which use a web service.

I handle much of the loading and saving using custom code, including -saveToURL:ofType:forSaveOperation:completionHandler:. +autosavesInPlace returns YES.

The problem I’m having: documents with the custom URL scheme aren’t restored on startup. Documents with the file URL scheme are – both regular documents saved to files, and untitled documents which are autosaved.

After leaving open server-based documents and quitting the app, no NSDocument methods appear to be called on restart. In particular, none of the four initializers is called:

  • –init
  • –initWithContentsOfURL:ofType:error:
  • –initForURL:withContentsOfURL:ofType:error:
  • –initWithType:error:

The NSDocumentController method -reopenDocumentForURL:withContentsOfURL:display:completionHandler: is not called either.

How and when are documents’ restorable state encoded? How and when are they decoded?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:25:30+00:00Added an answer on June 16, 2026 at 9:25 am

    NSDocument is responsible for encoding its restorable state in -encodeRestorableStateWithCoder:, and NSDocumentController is responsible for decoding documents’ restorable state and reopening the documents in +restoreWindowWithIdentifier:state:completionHandler:. Refer to the helpful comments in NSDocumentRestoration.h.

    When NSDocument encodes the URL, it appears to use the bookmark methods of NSURL. The problem is that these methods only work with file-system URLs. (It’s possible non-file URLs will encode, but they will not properly decode.)

    To fix the problem, override the encoding of NSDocument instances which use the custom scheme, and likewise, the decoding of those documents.

    NSDocument subclass:

    - (void) encodeRestorableStateWithCoder:(NSCoder *) coder {
        if ([self.fileURL.scheme isEqualToString:@"customscheme"])
            [coder encodeObject:self.fileURL forKey:@"MyDocumentAutoreopenURL"];
        else
            [super encodeRestorableStateWithCoder:coder];
    }
    

    NSDocumentController subclass:

    + (void) restoreWindowWithIdentifier:(NSString *) identifier
                                   state:(NSCoder *) state
                       completionHandler:(void (^)(NSWindow *, NSError *)) completionHandler {
    
        NSURL *autoreopenURL = [state decodeObjectForKey:@"MyDocumentAutoreopenURL"];
        if (autoreopenURL) {
            [[self sharedDocumentController]
             reopenDocumentForURL:autoreopenURL
             withContentsOfURL:autoreopenURL
             display:NO
             completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) {
    
                 NSWindow *resultWindow = nil;
                 if (!documentWasAlreadyOpen) {
    
                     if (![[document windowControllers] count])
                         [document makeWindowControllers];
    
                     if (1 == document.windowControllers.count)
                         resultWindow = [[document.windowControllers objectAtIndex:0] window];
                     else {
                         for (NSWindowController *wc in document.windowControllers)
                             if ([wc.window.identifier isEqual:identifier]) {
                                 resultWindow = wc.window;
                                 break;
                             }
                     }
                 }
                 completionHandler(resultWindow, error);
             }
             ];
        } else
            [super restoreWindowWithIdentifier:identifier
                                         state:state
                             completionHandler:completionHandler];
    }
    

    The behavior or the completion handler follows from Apple’s method comment in NSDocumentRestoration.h and should be roughly the same as super‘s.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application based on google map, in which I need to use
I'm developing a web based application built on codeigniter which will also have a
I have a NSDocument based application, which allows the user to send messages, after
We have a web application based on NSpring 1.2 and NHibernate 2 and use
I have a web application based on Django. I use the Scrapy Crawler to
I have a web application based on the sharp-architecture framework. It actually use NHibernate
I have typical web application based on PHP, HTML, and javascript. From an HTML
We have inherited an application (Java-based, running on WebLogic 10.3.5) that makes extensive use
I have one legacy web application based on struts2 (primarily using annotation). While debugging
We have a requirement to control access to our SaaS based web application based

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.