来自谷歌驱动器的客观c列表文件

时间:2013-01-28 12:49:35

标签: ios objective-c google-drive-api

正在开发ios应用程序,包括谷歌驱动器。在谷歌驱动器中尝试列出文件时,它会在单页本身中显示所有文件夹,子文件夹和子文件。这些是我的代码

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];

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

            [FilesFromGoogleDrive addObjectsFromArray:filesList.items
             ];

        };

我不想列出主页面中的所有文件。我需要类似于谷歌驱动器应用程序以顺序方式访问文件夹,子文件夹,子文件。从过去一周尝试这个,但没有好结果。所以请帮我如何访问文件夹,子文件夹。

4 个答案:

答案 0 :(得分:5)

如果要列出folderId标识的文件夹中的所有文件,可以使用以下代码(来自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);
          }
        }];
}

另一种选择是在其Parents集合中搜索folderId的文件:

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

例如,您可以使用以下搜索查询:

'1234567' in parents

其中'1234567'是您要为其列出文件的文件夹的ID。

答案 1 :(得分:0)

我希望这是你想要的。只需使用此代码,您就可以获得所需的代码。

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

答案 2 :(得分:0)

我是这样做的:

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

使用@“root”作为父目录从根目录列出文件,并使用子文件夹项的file.identifier作为列出文件的父项。要查询子文件夹中的文件,请使用以下2行来获取子文件夹中所有文件的父标识符。

GTLDriveFile subFolderItem;
currentFolderID = subFolderItem.identifier;

答案 3 :(得分:0)

要获取Google云端硬盘的所有文件列表,请使用以下代码:

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