Search code examples
objective-curluimanageddocument

How to retrieve an UIManagedDocument from a known url / file path?


My purpose is to retrieve a UIManagedDocument object from an known url and then open it.

Something like:

 UImanagedDocument *doc = retrieveDoc(url);

then, I can do something like:

 [doc openWithCompletionHandler:^(BOOL success){ ....}];

Believe me I did search apple's documentation there is a only method called initialise with a given url. Yes, I init create and save it before, and later I just need pick it up. Any way to do this?

Hope any one could give hints, thanks


Solution

  • There should be such a class method, but in apple's reference, I found the sample code for creating UIManagedDocument

     doc = [[UIManagedDocument alloc] initWithFileURL:docURL];
     if ([[NSFileManager defaultManager] fileExistsAtPath:[docURL path]]) {
    [doc openWithCompletionHandler:^(BOOL success){
        if (!success) {
            // Handle the error.
        }
    }];
    }
    else {
    [self addInitialData];
    [doc saveToURL:docURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
        if (!success) {
            // Handle the error.
        }
    }];
    

    So, basically, every time I want retrieve a UIManagedDocument from a given url, I must init it first and then open it. Am I right?

    Any way, this is the way what I found working till now.