ios - 本地保存后如何在 IOS 中加载 CSV

标签 ios objective-c quicklook

我有一个从 S3 帐户下载的 CSV 文件,我想使用 Quicklook 框架在我的 ios 应用程序中显示它。

我得到的错误在我的控制台中。它说

QLPreviewController's datasource shouldn't be nil at this point.

这行代码运行后出现 // Set data source [previewer setDataSource:self];
这是下载文件、保存文件然后使用 quicklook 加载的所有代码
-(void)showDocument
{
    NSString *stringURL = @"http://jornada.s3.amazonaws.com/Dust.csv";
    NSURL  *url = [NSURL URLWithString:stringURL];
    NSData *urlData = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];
    if ( urlData )
    {
        NSArray   *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString  *documentsDirectory = [paths objectAtIndex:0];

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

        //[urlData writeToFile:filePath atomically:YES];

        BOOL newFile = [[NSFileManager defaultManager] createFileAtPath:filePath contents:urlData attributes:nil];

        arrayOfDocuments = [[NSArray alloc] initWithObjects:
                            filePath, nil];


        QLPreviewController *previewer = [[QLPreviewController alloc] init];

        [self addSubview:previewer.view];

        // Set data source
        [previewer setDataSource:self];

        // Which item to preview
        [previewer setCurrentPreviewItemIndex:0];
    }

}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (NSInteger) numberOfPreviewItemsInPreviewController: (QLPreviewController *) controller
{
    return [arrayOfDocuments count];
}

/*---------------------------------------------------------------------------
 *
 *--------------------------------------------------------------------------*/
- (id <QLPreviewItem>)previewController: (QLPreviewController *)controller previewItemAtIndex:(NSInteger)index
{


    // Break the path into it's components (filename and extension)
    NSArray *fileComponents = [[arrayOfDocuments objectAtIndex: index] componentsSeparatedByString:@"."];

    NSArray *filePaths = [[fileComponents objectAtIndex:0] componentsSeparatedByString:@"/"];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[((NSString *)[filePaths objectAtIndex:[filePaths count]-1]) stringByAppendingString:(NSString*)[fileComponents objectAtIndex:1]]];

    // Use the filename (index 0) and the extension (index 1) to get path

    BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath];


    if (fileExists) {
        //
    }
    return [NSURL fileURLWithPath:filePath isDirectory:NO];
}

最佳答案

您不会将预览 Controller 的 View 添加到您的 View 中 - 您呈现预览 Controller 。您应该在设置数据源后执行此操作!所以,按照这个顺序:

QLPreviewController* preview = [QLPreviewController new];
preview.dataSource = self;
[self presentViewController:preview animated:YES completion:nil];

关于ios - 本地保存后如何在 IOS 中加载 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16660691/

相关文章:

ios - UITableViewCell 重新排序 : cell stops moving 10 pixels into the drag

ios - 第二个 UIWindow 中的方向更改

objective-c - 如何缓存弹出 Controller ?

xcode - 无法在 Xcode 下调试我的 quicklook 插件

macos - 如何在 Mac OS 10.7 中使用 QuickLook 注册新扩展?

ios - 为什么 QLPreviewController 上的 canPreviewItem 对于 iOS 13 上支持的文件格式(如 PDF)失败?

ios - 显示 UIAlert 会使应用程序崩溃吗?

ios - Swift 中使用 NSJSONSerialization.dataWithJSONObject 和 dataUsingEncoding 组成 HTTP body 的区别

iphone - 充当 NSUrlConnection 委托(delegate)的 NSObject 实例似乎不是孤立的

ios - 如果我在我的生产应用程序中使用第三方崩溃分析 sdk,它会抑制我的 iTunes 崩溃报告吗?