iPermanent
iPermanent

Reputation: 51

Dropbox api get image url list

I had put the MWPhotoBrowser into my dropbox app, however the browser had to use url for image array. Now I use the api but it only returns me metadata, to load the url it use delegate so much so I cannot guarantee that I had load all the urls, at the same time, the url only get the first one. So I ask if anyone else had do this work? Thanks very much.

Upvotes: 1

Views: 912

Answers (2)

Jatin Kathrotiya
Jatin Kathrotiya

Reputation: 539

You have to call loadStreamableURLForFile method of restClient with path of file

- (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];
        }
    }

}

And you get response of url in delegate method of restClient

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

}

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

}

Upvotes: 3

Jayaprada
Jayaprada

Reputation: 954

  • By Using Dropbox APi we can't get the url of a particular file or folder.

  • so what we need to do is,After geeting all the files,download that in background and save in documets directory

NSString *fileName = @"";//define filename acc to filename extension or date

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

NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];

   [self.restClient loadFile:dropboxPath intoPath:filePath];
  • After completion of download ,we can get URL of each and every file.

Use Below Method to get all the files as per appFolder Name

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

// list files found in the root dir of 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];
}

Upvotes: 0

Related Questions