【问题标题】:objective c list files from google drive来自谷歌驱动器的目标 c 列表文件
【发布时间】:2013-01-28 12:49:34
【问题描述】:

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

GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];

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

            [FilesFromGoogleDrive addObjectsFromArray:filesList.items
             ];

        };

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

【问题讨论】:

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


    【解决方案1】:

    如果要列出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。

    【讨论】:

    • 感谢您的回复。但在我的谷歌驱动器帐户中,我有 2 个文件夹,例如 Drive-->NewFolder-->Newfolder1 和一个文件夹。当我执行列表查询时,它会检索一页中的所有文件夹,包括子文件夹。我需要在谷歌驱动器帐户中列出。所以我需要做的。
    • 我们终于得到了获取主目录文件和文件夹的答案。为此,我们使用 query.q ="'root' in parents"
    【解决方案2】:

    我希望这是你想要的。只需使用此代码即可满足您的需求。

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

    【讨论】:

      【解决方案3】:

      我是这样做的:

      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;
      

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-16
          • 2013-01-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多