【问题标题】:UIDocument not openingUIDocument 打不开
【发布时间】:2012-08-28 16:37:28
【问题描述】:

使用UIDocument,我在openWithCompletionHandler: 中的完成处理程序永远不会被调用并且文档保持关闭状态。我不确定我做错了什么;一切看起来都很好,并且与我找到的示例代码相匹配。这就是我正在做的事情:

+ (NSString *)documentsDirectory {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    return basePath;
}

+ (NSURL *)documentsURL {
    NSString *documentsDirectory = [XKCDParser documentsDirectory];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"documentModel"];
    return [NSURL fileURLWithPath:filePath];
}

- (void)loadComicsAfterNumber:(NSInteger)comicNumber {
    if (comicNumber < 0) {
        if ([self.delegate respondsToSelector:@selector(errorLoadingComics:)])
            [self.delegate errorLoadingComics:[NSError errorWithDomain:@"Comic number cannot be less than 0" code:2 userInfo:nil]];
        return;
    }

    self.document = [[UIManagedDocument alloc] initWithFileURL:[XKCDParser documentsURL]];
    NSLog(@"document: %@", document);
    if ([[NSFileManager defaultManager] fileExistsAtPath:[[XKCDParser documentsURL] path]]) {
        NSLog(@"file exists at path");

        [document openWithCompletionHandler:^(BOOL success) {
            NSLog(@"document: %@", document);
            if (success)
                NSLog(@"Document opened sucessfully");
            if (!success)
                NSLog(@"Document did not open successfully");
        }];
        NSLog(@"2 document: %@", document);
    }
    else {
        [document saveToURL:[XKCDParser documentsURL] forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (!success) {
                NSLog(@"Could not create document at path: %@", [[XKCDParser documentsURL] path]);
                if ([self.delegate respondsToSelector:@selector(errorLoadingComics:)])
                    [self.delegate errorLoadingComics:[NSError errorWithDomain:[NSString stringWithFormat:@"Could not create document at path: %@", [[XKCDParser documentsURL] path]] code:4 userInfo:nil]];
            }
            else
                [self downloadComicsAfterNumber:comicNumber];
        }];
    }
    NSLog(@"3 document: %@", document);
}

控制台输出

2012-08-28 11:59:09.755 XKCD[12194:c07] document: <UIManagedDocument: 0xd2f90f0> fileURL: file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/CE8A9DC6-FB09-4426-BDCD-863F157FFA04/Documents/documentModel/ documentState: [Closed]
2012-08-28 11:59:09.757 XKCD[12194:c07] file exists at path
2012-08-28 11:59:09.757 XKCD[12194:c07] 2 document: <UIManagedDocument: 0xd2f90f0> fileURL: file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/CE8A9DC6-FB09-4426-BDCD-863F157FFA04/Documents/documentModel/ documentState: [Closed]
2012-08-28 11:59:09.761 XKCD[12194:c07] 3 document: <UIManagedDocument: 0xd2f90f0> fileURL: file://localhost/Users/User/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/CE8A9DC6-FB09-4426-BDCD-863F157FFA04/Documents/documentModel/ documentState: [Closed]

【问题讨论】:

  • self.document 是否被覆盖或被清除?文档很强大吧?
  • NSLog() 输出显示文档 fileURL 是一个目录。对吗?
  • 我也注意到了这一点,但我认为这正是 Core Data 处理它的方式。 +documentsURL 是我想要的,但它似乎被制作成一个目录。是的,文档很强大。

标签: iphone ios core-data objective-c-blocks


【解决方案1】:

尝试为您的文件指定一个带扩展名的名称。我通常使用.db,但任何事情都可以。现在系统可能认为你正在尝试打开一个目录,而实际上你想打开一个包(由 Core Data 创建)。

【讨论】:

  • 不幸的是,这并没有解决问题。在 Finder 中,它仍然显示为一个目录。
【解决方案2】:

在您的 .plist (LSTypeIsPackage) 中将“文档是一个包或捆绑包”设置为 YES 很重要,如 iOS 的基于文档的应用程序编程指南中所述 Declare a Document Type 部分。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2019-10-12
    • 2013-10-23
    • 2014-04-15
    相关资源
    最近更新 更多