Create folder with Google Drive SDK iOS

I’m trying to create a folder using the Google Drive SDK for iOS. From the Google Drive documentation here:

https://developers.google.com/drive/folder

  • Wiring up Outlet Collection Trouble
  • How can I pad an integer on the left with zeros?
  • Using UIPinchGestureRecognizer to scale uiviews in single direction
  • Default Date for UIDatePicker in iOS
  • Duplicating an XCode 4 Project
  • Doubletaptogo responsive menu not working on phone
  • It says that creating a folder is just like creating a ‘file’ with a special MIME type.

    GTLUploadParameters *uploadParameters = nil;
    
    self.driveFile = [GTLDriveFile object];
    self.driveFile.title = foldername;
    
    uploadParameters = [GTLUploadParameters new];
    uploadParameters.MIMEType = @"application/vnd.google-apps.folder";
    
    GTLQueryDrive *query = nil;
    query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
                                        uploadParameters:uploadParameters];
    
    
    [self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                              GTLDriveFile *updatedFile,
                                                              NSError *error) {
    

    But when I perform the query, I get:

    ‘NSInternalInconsistencyException’, reason: ‘upload data and fileHandle are mutually exclusive’

    This suggests I should be using one of the predefined GTLUploadParameters factory methods:

    + (GTLUploadParameters *)uploadParametersWithData:(NSData *)data
                                             MIMEType:(NSString *)mimeType;
    
    + (GTLUploadParameters *)uploadParametersWithFileHandle:(NSFileHandle *)fileHandle
                                                   MIMEType:(NSString *)mimeType;
    

    But neither of these are appropriate (or work, as I’ve tried). Has anyone successfully created folders using Google Drive on iOS?

    2 Solutions Collect From Internet About “Create folder with Google Drive SDK iOS”

    I do this:

        GTLDriveFile *folderObj = [GTLDriveFile object];
        folderObj.title = folderName;
        folderObj.mimeType = @"application/vnd.google-apps.folder";
    
        // To create a folder in a specific parent folder, specify the identifier
        // of the parent:
        // _resourceId is the identifier from the parent folder
        if (_resourceId.length && ![_resourceId isEqualToString:@"0"]) {
            GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
            parentRef.identifier = _resourceId;
            folderObj.parents = [NSArray arrayWithObject:parentRef];
        }
    
        GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folderObj uploadParameters:nil];
    
        // executeQuery:...
    

    Here’s another more complete variant:

    + (void)createFolderWithService:(GTLServiceDrive *)service
                         folderName:(NSString *)folderName
                           parentId:(NSString *)parentId
                    completionBlock:(void (^)(GTLServiceTicket *, GTLDriveFile *, NSError *))completionBlock {
      GTLDriveFile *folderObj = [GTLDriveFile object];
      folderObj.title = folderName;
      folderObj.mimeType = @"application/vnd.google-apps.folder";
    
      if (parentId.length && ![parentId isEqualToString:@"0"]) {
        GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
        parentRef.identifier = parentId;
        folderObj.parents = [NSArray arrayWithObject:parentRef];
      }
    
      GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folderObj uploadParameters:nil];
      [service executeQuery:query completionHandler:completionBlock];
    }