使用Google Drive SDK iOS创建文件夹内部文件夹

时间:2016-12-12 12:45:43

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

我尝试使用Google Drive SDK for iOS创建文件夹。 我使用如下代码,创建文件夹内部的主驱动器创建。

GTLDriveFile *folder = [GTLDriveFile object];
folder.title = @"Testing";
folder.mimeType = @"application/vnd.google-apps.folder";
NSString *parentId = @"Testing";

folder.parents = @[parentId];

// ... execute query


GTLQueryDrive *query1 = [GTLQueryDrive     queryForFilesInsertWithObject:folder uploadParameters:nil];
[self.driveService executeQuery:query1 completionHandler:^(GTLServiceTicket *ticket,
                                              GTLDriveFile *updatedFile,
                                              NSError *error)
{
    if (error == nil)
    {
        NSLog(@"Created folder %@",updatedFile.identifier);

         GTLDriveParentReference *parentReference = [GTLDriveParentReference object];

         parentReference.identifier = updatedFile.identifier;
         GTLDriveFile *subFolder = [GTLDriveFile object];
         subFolder.title = @"Testing12352";
         subFolder.mimeType = @"application/vnd.google-apps.folder";
         NSString *parentId = updatedFile.identifier;
         subFolder.parents =  @[parentReference];

        // ... execute query

        GTLQueryDrive *query1 = [GTLQueryDrive queryForFilesInsertWithObject:folder uploadParameters:nil];
        [self.driveService executeQuery:query1 completionHandler:^(GTLServiceTicket *ticket,
                                                                   GTLDriveFile *updatedFile,
                                                                   NSError *error)
         {
             if (error == nil)
             {
                  NSLog(@"Created folder %@",updatedFile.parents);
             }
         }];

工作完美。 Google驱动器文件夹内部文件夹创建..

1 个答案:

答案 0 :(得分:1)

如果您要在Testingroot Testing12352文件夹中创建Testing文件夹,那么对于Testing,您需要将parents设置为[@"root"]Testing12352您需要将测试identifier设置为parents

GTLDriveFile *folder = [GTLDriveFile object];
folder.title = @"Testing";
folder.mimeType = @"application/vnd.google-apps.folder";
NSString *parentId = @"root";

folder.parents = @[parentId];

// ... execute query



GTLQueryDrive *query1 = [GTLQueryDrive     queryForFilesInsertWithObject:folder uploadParameters:nil];
[self.driveService executeQuery:query1 completionHandler:^(GTLServiceTicket *ticket,
    GTLDriveFile *updatedFile,
    NSError *error)
    {
        if (error == nil)
        {
            NSLog(@"Created folder %@",updatedFile.identifier);



            GTLDriveFile *subFolder = [GTLDriveFile object];
            subFolder.title = @"Testing12352";
            subFolder.mimeType = @"application/vnd.google-apps.folder";
            NSString *parentId = updatedFile.identifier;
            subFolder.parents = @[parentId];

            // ... execute query

            GTLQueryDrive *query1 = [GTLQueryDrive queryForFilesInsertWithObject:subFolder uploadParameters:nil];
            [self.driveService executeQuery:query1 completionHandler:^(GTLServiceTicket *ticket,
            GTLDriveFile *updatedFile,
            NSError *error)
            {
                if (error == nil)
                {
                    NSLog(@"Created folder %@",updatedFile.parents);
                }
            }];

注意:请勿忘记将scope设为kGTLAuthScopeDrive