问题:我想从我的 Dropbox 帐户下载一个文件并使用快速查看来可视化它。
第一个解决方案:
1)使用Dropbox API restClient:
[[self restClient] loadFile:fullpath intoPath:finalpath];
2) 下载后使用 QLPreviewController 预览文件。
这个解决方案的问题是我不知道如何将下载与预览同步(使用快速查看文件需要在本地,所以我需要先下载它)。
我想出的(丑陋的)解决方法是设置一个警报(“缓存”)并使其持续任意长度的时间(比如说 12 秒,幻数......)。同时我暂停执行 10-12 秒(幻数):
[NSThread sleepForTimeInterval:12.0f];
...并希望在此时间间隔结束时文件已下载,以便我可以启动 QLPreviewController。
这是代码(丑陋,我知道....):
// Define Alert
UIAlertView *downloadAlert = [[UIAlertView alloc] initWithTitle:@"caching" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil] ;
// If file does not exist alert downloading is ongoing
if(![[NSFileManager defaultManager] fileExistsAtPath:finalpath])
{
// Alert Popup
[downloadAlert show];
//[self performSelector:@selector(isExecuting) withObject:downloadAlert afterDelay:12];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Here your non-main thread.
if(![[NSFileManager defaultManager] fileExistsAtPath:finalpath])
{
[NSThread sleepForTimeInterval:12.0f];
}
dispatch_async(dispatch_get_main_queue(), ^{
// Dismiss alert
[downloadAlert dismissWithClickedButtonIndex: -1 animated:YES];
//Here we return to main thread.
// We use the QuickLook APIs directly to preview the document -
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
// Push new viewcontroller, previewing the document
[[self navigationController] pushViewController:previewController animated:YES];
});
});
它确实有效(小文件和快速连接),但它不是最好的解决方案......
我认为最好的解决方案是将 NSURLSession 与 dropbox restClient 集成,以便使用此例程:
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration
delegate:nil
delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDownloadTask *task;
task = [session downloadTaskWithRequest:request
completionHandler:^(NSURL *localfile, NSURLResponse *response, NSErr or *error) {
/* yes, can do UI things directly because this is called on the main queue */ }];
[task resume];
但我不确定如何将它与 DropBox API 一起使用:有什么建议吗?
谢谢,dom