如何使用Dropbox Sync API iOS SDK从Dropbox下载文件

我使用的Dropbox同步API sdk它显示所有的文件夹和文件,但我们不能下载文件我使用这个代码:

DBFilesystem *filesystem; DBFile *file = [_filesystem openFile:info.path error:nil]; NSData *imagedata=[file readData:nil]; [fileMan createFileAtPath:Image_filePath contents:**data** attributes:nil]; NSLog(@"flao=%d",[file writeData:imagedata error:nil]); // it return 1 

我得到NSData,但我不能存储在文档目录中。 我检查下面的链接,但没有成功

Dropbox Sync API iOS将文件复制到应用程序文档

数据与DropBox API和iOS同步

请帮帮我。

Dropbox需要时间来caching文件,您可以设置一个块来监视caching何时完成:

 __block MyViewController *me = self; [self.dropboxFile addObserver:self block:^() { [me monitorDropboxFile:@"Sync"]; }]; 

在观察者的内部:

 - (void)monitorDropboxFile:(NSString *)caller { if (self.dropboxFile.status.cached) { ; // Good to go, the file is cached } else { ; // download in progress } } 

或者,有一个更简单的方法,使用readHandle

 (DBFile *)file; // ... DBError *dbError; NSFileHandle *fileHandle = [file readHandle:&dbError]; 

如Dropbox API头文件所述:

/ **返回文件的只读文件句柄。 如果文件没有被caching,则该方法将被阻塞,直到文件被下载。
@return如果文件可以被读取则返回一个文件句柄,如果发生错误则返回nil 。 * /

文件句柄返回时准备就绪。 但是,如果应用程序在文件下载期间必须响应,您可能希望将readHandle放在后台线程中。