主要归功于格雷格。
我正在使用 MBProgressHUD 直观地显示数据/图像/文件下载。
现在,在删除旧的 Dropbox SDK 的 DBRESTClient 委托后,这就是我的工作方式。
就我而言,首先,我使用以下代码列出了保管箱中的文件-
我正在将图像/文件下载到文档目录中。
为此,首先,我需要文件和文件夹的列表,因此,我通过调用getAllDropboxResourcesFromPath:
path 是保管箱根目录的方法来获取列表。
在我的 viewDidLoad 方法中,我得到了列表。
- (void)viewDidLoad
{
[super viewDidLoad];
[self getAllDropboxResourcesFromPath:@"/"];
self.list = [NSMutableArray alloc]init; //I have the list array in my interface file
}
-(void)getAllDropboxResourcesFromPath:(NSString*)path{
DBUserClient *client = [DBClientsManager authorizedClient];
NSMutableArray *dirList = [[NSMutableArray alloc] init];
[[client.filesRoutes listFolder:path]
setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderError *routeError, DBRequestError *networkError) {
if (response) {
NSArray<DBFILESMetadata *> *entries = response.entries;
NSString *cursor = response.cursor;
BOOL hasMore = [response.hasMore boolValue];
[self listAllResources:entries];
if (hasMore){
[self keepListingResources:client cursor:cursor];
}
else {
self.list = dirList;
}
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}];
}
- (void)keepListingResources:(DBUserClient *)client cursor:(NSString *)cursor {
[[client.filesRoutes listFolderContinue:cursor]
setResponseBlock:^(DBFILESListFolderResult *response, DBFILESListFolderContinueError *routeError,
DBRequestError *networkError) {
if (response) {
NSArray<DBFILESMetadata *> *entries = response.entries;
NSString *cursor = response.cursor;
BOOL hasMore = [response.hasMore boolValue];
[self listAllResources:entries];
if (hasMore) {
[self keepListingResources:client cursor:cursor];
}
else {
self.list = dirList;
}
} else {
NSLog(@"%@\n%@\n", routeError, networkError);
}
}];
}
- (void) listAllResources:(NSArray<DBFILESMetadata *> *)entries {
for (DBFILESMetadata *entry in entries) {
[dirList addObject:entry];
}
}
上面的代码将所有文件和文件夹作为DBFILESMetadata
类型对象存储在列表数组中。
现在我准备好下载了,但是我的文件很大,所以我需要显示我正在使用的下载进度MBProgressHUD
。
-(void)downloadOnlyImageWithPngFormat:(DBFILESMetadata *)file{
//adding the progress view when download operation will be called
self.progressView = [[MBProgressHUD alloc] initWithWindow:[AppDelegate window]]; //I have an instance of MBProgressHUD in my interface file
[[AppDelegate window] addSubview:self.progressView];
//checking if the content is a file and if it has png extension
if ([file isKindOfClass:[DBFILESFileMetadata class]] && [file.name hasSuffix:@".png"]) {
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* localPath = [documentsPath stringByAppendingPathComponent:file.pathLower];
BOOL exists=[[NSFileManager defaultManager] fileExistsAtPath:localPath];
if(!exists) {
[[NSFileManager defaultManager] createDirectoryAtPath:localPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSURL *documentUrl = [NSURL fileURLWithPath:localPath];
NsString *remotePath = file.pathLower;
DBUserClient *client = [DBClientsManager authorizedClient];
[[[client.filesRoutes downloadUrl:remotePath overwrite:YES destination:documentUrl] setResponseBlock:^(DBFILESFileMetadata *result, DBFILESDownloadError *routeError, DBRequestError *networkError, NSURL *destination) {
if(result){
NSLog(@"File Downloaded");
}
}] setProgressBlock:^(int64_t bytesDownloaded, int64_t totalBytesDownloaded, int64_t totalBytesExpectedToDownload) {
[self setProgress:[self calculateProgress:totalBytesExpectedToDownload andTotalDownloadedBytes:totalBytesDownloaded]];
}];
}
- (void) setProgress:(CGFloat) progress {
[self.progressView setProgress:progress];
}
- (CGFloat) calculateProgress:(long long)totalbytes andTotalDownloadedBytes:(long long)downloadedBytes{
double result = (double)downloadedBytes/totalbytes;
return result;
}
希望这可以帮助其他人。再次非常感谢 Greg 给我的提示。