我被要求将发送到Google云端硬盘帐户的文件写入使用以下代码创建的预定义文件夹
(void) createGoogleFolder
{
GTLDriveFile *folder = [GTLDriveFile object];
folder.title = @"ICA Voice Memo";
folder.mimeType = @"application/vnd.google-apps.folder";
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folder uploadParameters:nil];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
if (error != nil)
{
NSString *err = error;
}
}];
}
我的编写mp4文件的代码是谷歌驱动器
-(void) writeToGoogleDrive:(NSString *)modifiedFileDateString
{
[self createGoogleFolder];
NSString *title = modifiedFileDateString;
GTLDriveFile *metadata = [GTLDriveFile object];
metadata.title = title;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *filePath = [docDirectory stringByAppendingPathComponent:modifiedFileDateString];
NSError *error = nil;
NSData *data = [NSData dataWithContentsOfFile:filePath options:0 error:&error];
if(data != nil && error==nil) {
GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:@"audio/mp4"];
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:metadata uploadParameters:uploadParameters];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
if (error != nil) {
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Upload Error"
message: [error localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
}
我想将数据写入Google驱动器“ICA语音备忘录”中创建的新文件夹。
如何修改代码才能执行此操作?
答案 0 :(得分:0)
几天后我终于解决了这个问题。首先,我有一个方法,使用当前文件夹Id检查文件夹的存在,如果它是空白我使用下面的代码
-(void) createGoogleFolder
{
GTLDriveFile *folder = [GTLDriveFile object];
folder.title = @"ICA Voice Memo";
folder.mimeType = @"application/vnd.google-apps.folder";
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folder uploadParameters:nil];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFolder,
NSError *error)
{
if (error == nil)
{
uploadFolderID = updatedFolder.identifier;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *path = [docDirectory stringByAppendingPathComponent:@"Cloud.plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
[data setObject:uploadFolderID forKey:@"GoogleFolderID"];
[data writeToFile: path atomically:YES];
}
}];
}
之后我使用文件夹Id作为我的父ID,并以正常方式添加文件。