3

NSManagedDocument作成後に開こうとしています。ドキュメントを数回正常に開いた後、ドキュメントを開くことができないようです。ドキュメント名を変更するか、アプリを再度削除/コピーすると、機能します。以下のメソッドは、アプリケーションが起動するたびにアプリ デリゲートで呼び出されます。

-(void) loadDataDocument {

    NSURL *documentURL= [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                             inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"TestDataDocument1"];
    self.document = [[UIManagedDocument alloc] initWithFileURL:documentURL];


    if(![[NSFileManager defaultManager] fileExistsAtPath:[documentURL path]]) {
    [self.document saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        if (success) {
            [self documentIsReady];
        }else {
            NSLog(@"Tried to create new file. Cannot open document");
        }
    }];
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:^(BOOL success) {
            if (success) {
               [self documentIsReady];
            }else {
               NSLog(@"Document was Closed. Cannot open document");
            }
        }];
    } else {
        [self documentIsReady];
    }

}

4

1 に答える 1

0
-(void) loadDataDocument {

    NSString * fileUrl = [NSString stringWithFormat:@"%@abc.txt",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]];
    NSURL *documentURL= [NSURL URLWithString:fileUrl];

    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:documentURL];

    if(![[NSFileManager defaultManager] fileExistsAtPath:[documentURL path]]) {
        [document saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                NSManagedObjectContext * _context;
                _context = document.managedObjectContext;

            }
        }];
    } else if (document.documentState == UIDocumentStateClosed) {
             NSLog(@"document.documentState == UIDocumentStateClosed");
        NSString *fileDataStr = [[NSString alloc]initWithContentsOfURL:documentURL encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",fileDataStr);
    } else {
        NSManagedObjectContext * _context;
        _context = document.managedObjectContext;

    }

}

Note: 'UIManagedDocument can only read documents that are file packages'

于 2013-03-27T20:00:19.577 に答える