首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIDocument未打开

UIDocument未打开
EN

Stack Overflow用户
提问于 2012-08-28 16:37:28
回答 2查看 740关注 0票数 1

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

代码语言:javascript
复制
+ (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);
}

控制台输出

代码语言:javascript
复制
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]
EN

回答 2

Stack Overflow用户

发布于 2012-08-30 18:13:07

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

票数 0
EN

Stack Overflow用户

发布于 2014-10-31 18:25:20

在您的.plist (LSTypeIsPackage)中,重要的是将“文档是一个包或包”设置为“是”,正如基于文档的应用程序编程指南iOS 声明文档类型一节中提到的那样。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12163743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档