Dropbox api获取图片网址列表

问题描述 投票:1回答:2

我已将MWPhotoBrowser放入我的Dropbox应用程序,但浏览器必须使用url进行图像数组。现在我使用api但它只返回我的元数据,加载它使用委托的url这么多,所以我不能保证我已经加载了所有的url,同时,url只获得第一个。所以我问其他人是否做过这项工作?非常感谢。

ios dropbox
2个回答
3
投票

你必须用文件路径调用loadStreamableURLForFilerestClient方法

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {

    for (DBMetadata *node in metadata.contents) {
        if (node.isDirectory) {
            [self.restClient loadMetadata: node.path];
        } else { 
            [self.restClient loadStreamableURLForFile:node.path];
        }
    }

}

并且你在restClient的委托方法中获得了url的响应

- (void)restClient:(DBRestClient*)restClient loadedStreamableURL:(NSURL*)url forFile:(NSString*)path {

}

- (void)restClient:(DBRestClient*)restClient loadStreamableURLFailedWithError:(NSError*)error {

}

0
投票
  • 通过使用Dropbox APi,我们无法获取特定文件或文件夹的URL。
  • 所以我们需要做的是,在获取所有文件后,在后台下载并保存在文档目录中

NSString * fileName = @“”; //根据文件扩展名或日期定义文件名

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

   [self.restClient loadFile:dropboxPath intoPath:filePath];
  • 下载完成后,我们可以获取每个文件的URL。

使用Below Method按照appFolder名称获取所有文件

+ (NSURL*)appRootURL
{
    NSString *url = [NSString stringWithFormat:@"https://api.dropbox.com/1/metadata/dropbox/%@",appFolder];
    NSLog(@"listing files using url %@", url);
    return [NSURL URLWithString:url];
}

//列出在appFolder的根目录中找到的文件

- (void)notesOnDropbox
{
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  // 1
  NSURL *url = [Dropbox appRootURL];

  // 2
  NSURLSessionDataTask *dataTask =
  [self.session dataTaskWithURL:url
              completionHandler:^(NSData *data,
                                  NSURLResponse *response,
                                  NSError *error) {
                if (!error) {
                  // TODO 1: More coming here!
                  // 1
                  NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response;
                  if (httpResp.statusCode == 200) {

                    NSError *jsonError;

                    // 2
                    NSDictionary *notesJSON =
                    [NSJSONSerialization JSONObjectWithData:data
                                                    options:NSJSONReadingAllowFragments
                                                      error:&jsonError];

                    NSMutableArray *notesFound = [[NSMutableArray alloc] init];

                    if (!jsonError) {                    
                      // TODO 2: More coming here!
                      // 1
                      NSArray *contentsOfRootDirectory = notesJSON[@"contents"];

                      for (NSDictionary *data in contentsOfRootDirectory) {
                        if (![data[@"is_dir"] boolValue]) {
                          DBFile *note = [[DBFile alloc] initWithJSONData:data];
                          [notesFound addObject:note];
                        }
                      }

                      [notesFound sortUsingComparator:
                       ^NSComparisonResult(id obj1, id obj2) {
                         return [obj1 compare:obj2];
                       }];

                      self.notes = notesFound;

                      // 6
                      dispatch_async(dispatch_get_main_queue(), ^{
                        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                        [self.tableView reloadData];
                      });

                    }
                  }

                }
              }];

  // 3  
  [dataTask resume];
}
© www.soinside.com 2019 - 2024. All rights reserved.