iOS中的Google Drive集成可上传PDF文件

时间:2016-08-01 07:00:42

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

我正在开发iOS 8中的应用程序,并通过安装以下pod来集成Google Drive:

pod platform :ios, '8.0'

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

当我放置代码上传PDF文件时,我收到以下错误:

  
      
  1. 财产'标题'在#GT; GTLDriveFile *'
  2. 类型的对象上找不到   
  3. 使用未声明的标识符> GTLDriveParentReference'。
  4.   
  5. 没有已知的选择器类方法queryForFilesInsertWithObject:uploadParameters:'。
  6.   

这是我的代码:

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

2 个答案:

答案 0 :(得分:1)

  1. 使用driveFile.name代替driveFile.title

  2. 没有GTLDriveParentReference类,因此请删除这些行。

  3. 使用queryForFilesCreateWithObject代替queryForFilesInsertWithObject

答案 1 :(得分:0)

我创建了完整的方法来通过iOS中的谷歌硬盘上传PDF文件

-(void)sharePDFFileOnGoogleDrive{
GTLServiceDrive *driveService;
driveService = [[GTLServiceDrive alloc] init];
driveService.authorizer = self.authToken;
GTLDriveFile *metadata = [GTLDriveFile object];
metadata.originalFilename = @"pdf-sample.pdf";
metadata.title = @"PDF file Name";
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"pdf-sample" ofType:@"pdf"];
NSString *mimeType = @"application/pdf";
NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:filePath];
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithFileHandle:file
                                                                                   MIMEType:mimeType];
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:metadata
                                                   uploadParameters:uploadParameters];
[driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                     GTLDriveFile *updatedFile,
                                                     NSError *error) {
    if (error == nil) {
        NSLog(@"File %@", updatedFile);
    } else {
        NSLog(@"An error occurred: %@", error);
    }
    }];
}