Asokan R
Asokan R

Reputation: 143

objective c list files from google drive

Am developing ios app including google drive. In google drive when am trying to list files, it displays all folders, subfolders, subfiles in single page itself. These is my code

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];

    [driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                         GTLDriveFileList *filesList,
                                                         NSError *error) {
if (error == nil) {

            [FilesFromGoogleDrive addObjectsFromArray:filesList.items
             ];

        };

I dont want to list all files in main page. I need similar to google drive application for accessing folders, subfolders, subfiles in sequential way.Am trying this from past one week but there is no good result. So please help me how to access folders, subfolders.

Upvotes: 6

Views: 4945

Answers (4)

Himanshu Mahajan
Himanshu Mahajan

Reputation: 4799

To fetch the list of all files of Google Drive, use below code:

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = [NSString stringWithFormat:@"'%@' IN parents", @"root"];

[self.serviceDrive executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFileList *files,
                                                          NSError *error) {

    if (error == nil)
    {
        NSMutableArray * driveFiles = [[NSMutableArray alloc] init];
        [driveFiles addObjectsFromArray:files.items];

        for (GTLDriveFile *file in driveFiles)
            NSLog(@"File is %@", file.title);

    }
    else
    {
        NSLog(@"An error occurred: %@", error);
    }
}];

Upvotes: 0

Kumar Summan
Kumar Summan

Reputation: 236

Here is how I did it:

NSString* currentFolderID = @"root";//folder id for the Google Drive root directory
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
NSString* queryQ = [NSString stringWithFormat: @"trashed = false and '%@' in parents", currentFolderID];
query.q  = queryQ;
[_googleService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                                                GTLDriveFileList *files,
                                                                                NSError *error)
                   {
                       if (error == nil)
                       {
                            [self listFiles: files.items];
 }

Use @"root" as the parent to list files from the root directory, and use file.identifier of the subfolder item as the parent to list files from. To query files from a subfolder, use the following 2 lines to get the parent identifier for all files in the subfolder.

GTLDriveFile subFolderItem;
currentFolderID = subFolderItem.identifier;

Upvotes: 0

loganathan
loganathan

Reputation: 2086

I hope this is one that you want. Just use this code this one will give you what do you need.

-(void)getFileListFromSpecifiedParentFolder {
        GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:<some_folder_id or root>];
        query2.maxResults = 1000;

        // queryTicket can be used to track the status of the request.
        [self.driveService executeQuery:query2
            completionHandler:^(GTLServiceTicket *ticket,
                            GTLDriveChildList *children, NSError *error) {
                NSLog(@"\nGoogle Drive: file count in the folder: %d", children.items.count);
                //incase there is no files under this folder then we can avoid the fetching process
                if (!children.items.count) {
                    return ;
                }

                if (error == nil) {
                    for (GTLDriveChildReference *child in children) {

                        GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];

                        // queryTicket can be used to track the status of the request.
                        [self.driveService executeQuery:query
                            completionHandler:^(GTLServiceTicket *ticket,
                                                GTLDriveFile *file,
                                                NSError *error) {

                                                NSLog(@"\nfile name = %@", file.originalFilename);
                                                }];
                    }
                }
            }];
    }

Upvotes: 0

Claudio Cherubino
Claudio Cherubino

Reputation: 15024

If you want to list all files in a folder identified by folderId, you can use the following code (from https://developers.google.com/drive/v2/reference/children/list):

+ (void)printFilesInFolderWithService:(GTLServiceDrive *)service
                             folderId:(NSString *)folderId {
  // The service can be set to automatically fetch all pages of the result. More
  // information can be found on https://code.google.com/p/google-api-objectivec-client/wiki/Introduction#Result_Pages.
  service.shouldFetchNextPages = YES;

  GTLQueryDrive *query =
    [GTLQueryDrive queryForChildrenListWithFolderId:folderId];
  // queryTicket can be used to track the status of the request.
  GTLServiceTicket *queryTicket =
    [service executeQuery:query
        completionHandler:^(GTLServiceTicket *ticket,
                            GTLDriveChildList *children, NSError *error) {
          if (error == nil) {
            for (GTLDriveChildReference *child in children) {
              NSLog(@"File Id: %@", child.identifier);
            }
          } else {
            NSLog(@"An error occurred: %@", error);
          }
        }];
}

Another option is to search for files having folderId in their Parents collection:

https://developers.google.com/drive/search-parameters

For instance, you can have a search query like the following:

'1234567' in parents

Where '1234567' is the id of the folder you want to list files for.

Upvotes: 5

Related Questions