Femina Brahmbhatt Femina Brahmbhatt - 1 year ago 295
Objective-C Question

Google Drive integration in iOS to upload PDF file

I am developing an app in iOS 8 and I have integrated Google Drive by installing the following pods:

pod platform :ios, '8.0'

pod 'GoogleAPIClient/Drive', '~> 1.0.0'
pod 'GTMOAuth2', '~> 1.1.0'

When I place code to upload a PDF file, I am getting the following errors:

  1. Property 'title' not found on object of type 'GTLDriveFile *'

  2. Use of undeclared identifier 'GTLDriveParentReference'.

  3. No known class method for selector 'queryForFilesInsertWithObject:uploadParameters:'.

This is my code:

GTLDriveFile *driveFile = [GTLDriveFile object];

driveFile.mimeType = @"application/pdf";
driveFile.originalFilename = @"database.pdf";
driveFile.name = @"database.pdf";
driveFile.size = fileSizeNumber;
NSString *parentId = @"root";
driveFile.parents = @[parentId];

NSData *fileContent = [[NSData alloc] initWithContentsOfFile:fileName];
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"application/pdf"];

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = folderIdentifier; // identifier property of the folder
driveFile.parents = @[ parentRef ];

GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:driveFile uploadParameters:uploadParameters];

[self.service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
if (error == nil) {
NSLog(@"\n\nfile uploaded into google drive\\<my_folder> foler");
} else {
NSLog(@"\n\nfile uplod failed google drive\\<my_folder> foler");

Answer Source
  1. Use driveFile.name instead of driveFile.title

  2. There's no GTLDriveParentReference class, so remove these lines.

  3. Use queryForFilesCreateWithObject instead of queryForFilesInsertWithObject

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download