Search code examples
ioslistviewdownloaddropboxphotos

Download a photo using dropbox iOS SDK


I need to show users list of all photos in Dropbox and give a possibility to use them (to download in project). Simplify like user pick photo from his photo album. I'm using Dropbox SDK v1.3.3.

UPDATE:

This is what I done so far:

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
    if (metadata.isDirectory) {
        // Available files extensions
        NSArray *validExtensions = [NSArray arrayWithObjects:@"jpg", @"jpeg", @"png", nil];
        // Local path
        NSArray *libraryPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString *LibraryDirectory = [libraryPaths objectAtIndex:0];
        LibraryDirectory = [LibraryDirectory stringByAppendingString:@"/Dropbox/"];
        NSString *localPath = @"";

        if (![[NSFileManager defaultManager] fileExistsAtPath:LibraryDirectory]){
            NSError* error;
            if(  [[NSFileManager defaultManager] createDirectoryAtPath:LibraryDirectory withIntermediateDirectories:NO attributes:nil error:&error])
                ;// success
            else {
                NSLog(@"[%@] ERROR: attempting to write create Dropbox directory", [self class]);
                NSAssert( FALSE, @"Failed to create directory maybe out of disk space?");
            }
        }
        // Work with dropbox files array
        for (DBMetadata *file in metadata.contents) {
            NSString* extension = [[file.path pathExtension] lowercaseString];
            if(![file isDirectory] && [validExtensions indexOfObject:extension]!=NSNotFound) {
                localPath = [LibraryDirectory stringByAppendingPathComponent:[file filename]];
                // Add file to Dropbox Directory
                BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:localPath];
                if(!fileExists) [[self restClient] loadFile:file.path intoPath:localPath];
            }
        }
    }
}

Just left to show list of photos from Dropbox local folder. Maybe someone know how to do that? I want it to be like from Photo Album:

- (void)getDropboxPhotoList:(NSString *)path {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    /* source must be not from gallery, but from local folder */
    //imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;

    UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [pop presentPopoverFromBarButtonItem:(UIBarButtonItem *)dropboxButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
    popover = pop;
    [imagePicker release];
}

Solution

  • The Dropbox API shows some sample code for listing the files in a given folder:

    List files in a folder

    List files in a folder You can list the files in folder you just uploaded to with the following call:

    [[self restClient] loadMetadata:@"/"];
    

    The rest client will call your delegate with one of the following callbacks:

    - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
        if (metadata.isDirectory) {
            NSLog(@"Folder '%@' contains:", metadata.path);
        for (DBMetadata *file in metadata.contents) {
            NSLog(@"\t%@", file.filename);
        }
        }
    }
    
    - (void)restClient:(DBRestClient *)client
        loadMetadataFailedWithError:(NSError *)error {
    
        NSLog(@"Error loading metadata: %@", error);
    }
    

    Metadata objects are how you get information on files and folders in a user's Dropbox. Calling loadMetadata: on / will load the metadata for the root folder, and since it's a folder the contents property will contain a list of files and folders contained in that folder. It's advisable to keep this data around so that the next time you want to do something with a file, you can compare its current metadata to what you have stored to discern whether the file has been changed. Check out DBMetadata.h to see all the information metadata objects have.