When -initForURL:withContentsOfURL:ofType:error:
is called with nil
as the initial URL, that's usually a signal the the sourceURL
is going to be an unsaved Autosave file, which if you don't eventually save will be deleted (and if you do save, it'll be moved).
I would suggest that you initialize the document and then use one of the read
methods of document to read in the contents and override the empty document. If you want those to be easily abandoned after doing so (i.e. no save prompt if you don't make changes to the template document), you may need to set NSChangeCleared
using updateChangeCount:
.
For example (inserted in place of your current Document init lines:
NSError *error;
Document *document = [[Document alloc] initWithType: extension error: &error];
if (document) {
// load template
if (![document readFromURL: sourceURL ofType: extension error: &error]) {
// do something appropriate to abort the load
}
[document updateChangeCount: NSChangeCleared]; // don't prompt for save w/o changes
}
return document;