UIDocumentInteractionController"invalid scheme(null)"

Jor*_*416 22 excel preview nsdata ios uidocumentinteraction

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

'UIDocumentInteractionController: invalid scheme (null).  Only the file scheme is supported.'
Run Code Online (Sandbox Code Playgroud)

我用下一个代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

这两种方法都在viewcontroller中实现,我想在其中呈现预览.我很难知道我在urlData对象中获取数据,因为当我输入断点时,控制台说它包含6144个字节,这正是要下载的文档的大小.

我一直在寻找安静的一段时间,但我似乎无法弄清楚如何解决这个问题.我已经尝试将link对象作为documentInteractionController的源放入,但随后错误更改为:

'UIDocumentInteractionController: invalid scheme (http).  Only the file scheme is supported.'
Run Code Online (Sandbox Code Playgroud)

任何关于如何克服这个问题的评论都将受到高度赞赏

Che*_*Che 55

尝试使用

// objC
file = [NSURL fileURLWithPath:filePath];
// swift
file = URL.init(fileURLWithPath: filePath)
Run Code Online (Sandbox Code Playgroud)

代替

//objC
file = [NSURL URLWithString:filePath];
// swift
file = URL.init(string: filePath)
Run Code Online (Sandbox Code Playgroud)