如何使用Google Drive SDK for iPhone在Google云端硬盘上创建文件夹?

时间:2012-12-14 05:45:46

标签: iphone objective-c xcode google-drive-api

我正在使用适用于iPhone的Google Drive SDK并尝试在“TestAudio”文件夹中上传音频文件。如果未在Google驱动器中创建“TestAudio”文件夹,则首先创建该文件夹,之后我的音频应仅存储到该文件夹。除文件夹创建外,每件事都在工作。请朋友帮帮忙吗?

我正在使用以下代码上传音频文件。

GTLUploadParameters *uploadParameters = nil;

NSString *soundFilePath = [[NSBundle mainBundle]
                           pathForResource:@"honey_bunny_new"
                           ofType:@"mp3"];

if (soundFilePath) {
    NSData *fileContent = [[NSData alloc] initWithContentsOfFile:soundFilePath];
    uploadParameters = [GTLUploadParameters uploadParametersWithData:fileContent MIMEType:@"audio/mpeg"];
}

self.driveFile.title = self.updatedTitle;
GTLQueryDrive *query = nil;
if (self.driveFile.identifier == nil || self.driveFile.identifier.length == 0) {
    // This is a new file, instantiate an insert query.
    query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
                                        uploadParameters:uploadParameters];
} else {
    // This file already exists, instantiate an update query.
    query = [GTLQueryDrive queryForFilesUpdateWithObject:self.driveFile
                                                  fileId:self.driveFile.identifier
                                        uploadParameters:uploadParameters];
}
UIAlertView *alert = [DrEditUtilities showLoadingMessageWithTitle:@"Saving file"
                                                         delegate:self];

[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                          GTLDriveFile *updatedFile,
                                                          NSError *error) {
[alert dismissWithClickedButtonIndex:0 animated:YES];
if (error == nil) {
    self.driveFile = updatedFile;
    self.originalContent = [self.textView.text copy];
    self.updatedTitle = [updatedFile.title copy];
    [self toggleSaveButton];
    [self.delegate didUpdateFileWithIndex:self.fileIndex
                                    driveFile:self.driveFile];
    [self doneEditing:nil];
} else {
    NSLog(@"An error occurred: %@", error);
    [DrEditUtilities showErrorMessageWithTitle:@"Unable to save file"
                                           message:error.description
                                          delegate:self];
}
}];

1 个答案:

答案 0 :(得分:1)

我没有看到您创建文件夹的代码,但我自己也遇到了与文件夹创建相同的问题。如您所知,mimeType必须是“application / vnd.google-apps.folder”。如果uploadParametersWithData的NSData参数为nil,我遇到断言失败。所以我尝试了一个零长度的NSData对象,但失败了。使用1字节的NSData对象也失败了。诀窍是使用uploadParameters:nil调用queryForFilesUpdateWithObject。然后文件夹创建工作正常。我还发现在末尾显示的Objective-C代码:

https://developers.google.com/drive/v2/reference/files/insert

不正确。 file.parents应如下所示:

GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = parentID;
if (parentID.length>0) file.parents = [NSArray arrayWithObjects:parentRef,nil];