我正在开发iOS 8中的应用程序,并通过安装以下pod来集成Google Drive:
pod platform :ios, '8.0'
pod 'GoogleAPIClient/Drive', '~> 1.0.0'
pod 'GTMOAuth2', '~> 1.1.0'
当我放置代码上传PDF文件时,我收到以下错误:
- 财产'标题'在#GT; GTLDriveFile *'
类型的对象上找不到- 使用未声明的标识符> GTLDriveParentReference'。
- 没有已知的选择器类方法queryForFilesInsertWithObject:uploadParameters:'。
醇>
这是我的代码:
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");
}
}];
答案 0 :(得分:1)
使用driveFile.name
代替driveFile.title
没有GTLDriveParentReference
类,因此请删除这些行。
使用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);
}
}];
}