6

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

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];

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

            [FilesFromGoogleDrive addObjectsFromArray:filesList.items
             ];

        };

我不想列出主页中的所有文件。我需要类似于 google drive 应用程序来按顺序访问文件夹、子文件夹、子文件。我从过去一周开始尝试这个,但没有好的结果。所以请帮助我如何访问文件夹,子文件夹。

4

4 回答 4

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

另一种选择是搜索folderId其父母收藏中的文件:

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

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

'1234567' in parents

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

于 2013-01-28T22:34:50.593 回答
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);
                                                }];
                    }
                }
            }];
    }
于 2013-01-30T12:10:10.310 回答
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;
于 2013-04-30T19:06:54.167 回答
0

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

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);
    }
}];
于 2015-12-10T16:13:20.733 回答