UIDocumentInteractionController "invalid scheme (null)" 文档交互控制器“无效方案(空)”

22

我正在尝试使用UIDocumentInteractionController预览一个文档。 这个文档是一个.xls文件,我的应用程序从网络上下载。我一直收到这个错误:

'UIDocumentInteractionController: invalid scheme (null).  Only the file scheme is supported.'

我使用下面的代码:

- (void) webServiceController:(WebServiceController *)webServiceController returnedArray:(NSMutableArray *)array {
    if ([webServiceController.webRequest isEqualToString: @"generateExcelFileForEmployee"]) {
        // start previewing the document at the current section index
        NSString *link = [[NSString stringWithString:array[0]] stringByReplacingOccurrencesOfString:@"AdminFunctions.php" withString:@"AdminFunctions.xls"];
        link = [[[link stringByReplacingOccurrencesOfString:@"\\" withString:@""]stringByReplacingOccurrencesOfString:@"/public/sites/" withString:@"http://"]stringByReplacingOccurrencesOfString:@"\"\\\"" withString:@""];
        NSLog(@"%@", link);
        NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:link]];

        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

        NSString  *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"AdminFunctions.xls"];

        BOOL isWriteSuccess = [urlData writeToFile:filePath atomically:YES];
        NSLog(@"%@", filePath);

        if(isWriteSuccess) //success
        {
            file = [NSURL fileURLWithPath:filePath];

            self.fileView = [self setupControllerWithURL:file usingDelegate:self];

            [self.fileView presentPreviewAnimated:YES];
        }
        else
        {
           NSLog(@"file not written");
        }
    }
}

- (UIDocumentInteractionController *) setupControllerWithURL: (NSURL*) fileURL usingDelegate: (id <UIDocumentInteractionControllerDelegate>) interactionDelegate {

    UIDocumentInteractionController *interactionController =
    [UIDocumentInteractionController interactionControllerWithURL: fileURL];
    interactionController.delegate = interactionDelegate;

    return interactionController;
}

- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {
    return self;
}

这两种方法都在视图控制器中实现,我想在其中呈现预览。我知道我正在通过 urlData 对象获取数据,因为当我设置断点时,控制台会显示它包含6144个字节,这恰好是要下载的文档大小。

我已经寻找了相当长的时间,但似乎无法解决这个问题。我已经尝试将 link 对象作为文档交互控制器的源代码,但是错误消息变成了:

'UIDocumentInteractionController: invalid scheme (http).  Only the file scheme is supported.'
任何关于如何克服这个问题的评论将不胜感激。

你是否检查过文件是否已成功写入,因为你在UIDocumentInteractionController中使用了相同的文件路径? - Paresh Navadiya
我唯一检查这个的方法是通过nslogging文件路径,但这并不能真正说明文件是否被存储了,对吧?这是我第一次实现文档交互控制器,你能否解释一下如何检查这个问题? - Joris416
我已经编辑了你的问题,并添加了文件写入成功或失败的验证。现在请替换它并尝试一下,看看会发生什么? - Paresh Navadiya
谢谢您的建议!我在我的应用程序中实现了它并进行了测试,但我仍然遇到了相同的错误,这意味着文件实际上已经被写入。 - Joris416
1个回答

55

尝试使用

// objC
file = [NSURL fileURLWithPath:filePath];
// swift
file = URL.init(fileURLWithPath: filePath)

代替

//objC
file = [NSURL URLWithString:filePath];
// swift
file = URL.init(string: filePath)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接