1

What would be the best way to always ensure the file saved to disk (or iCloud) contains the default file extension for our document format in an NSDocument based Cocoa app?

Background:
Users can load legacy files into the app that still use Type-Creator-Codes.

With auto-saving enabled in our app we need to make sure the file always has the file extension added as soon as it's written back to the disk (following any kind of changes) with our Cocoa app - or the app won't be able to open it (with now neither the type-creator-code nor the file extension).

cacau
  • 3,606
  • 3
  • 21
  • 42

1 Answers1

2

If I got it right I'd overwrite NSDocumentController's open method

- (void)openDocumentWithContentsOfURL:(NSURL *)url 
                              display:(BOOL)displayDocument 
                    completionHandler:(void (^)(NSDocument *document, 
                                                BOOL documentWasAlreadyOpen, 
                                                NSError *error))completionHandler {
    if(!url.pathExtension isEqualToString:@"XYZ")
        url = url URLByAppendingPathExtension:@"XYZ"];
    [super openDocumentWithContentsOfURL:url 
                                 display:displayDocument 
                       completionHandler:completionHandler];
}

NOTE: written inline

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • Hmm, interesting - can't we get to the URL in a similar way from `NSDocument`? Like overriding `readFromURL:ofType:error:` instead (*currently using `readFromData:ofType:error:`*)? Do we need to go straight to `NSDocumentController`? – cacau Oct 15 '14 at 08:19
  • yes the document would know its url BUT the controller would still associate it with the old url. you really should overwrite the controller – Daij-Djan Oct 15 '14 at 08:24