我正在尝试使用Google Drive SDK for iOS创建一个文件夹。从此处的Google云端硬盘文档:
https://developers.google.com/drive/folder
它表示创建文件夹就像创建一个具有特殊MIME类型的“文件”一样。
GTLUploadParameters *uploadParameters = nil;
self.driveFile = [GTLDriveFile object];
self.driveFile.title = foldername;
uploadParameters = [GTLUploadParameters new];
uploadParameters.MIMEType = @"application/vnd.google-apps.folder";
GTLQueryDrive *query = nil;
query = [GTLQueryDrive queryForFilesInsertWithObject:self.driveFile
uploadParameters:uploadParameters];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *updatedFile,
NSError *error) {
但是当我执行查询时,我得到:
'NSInternalInconsistencyException',原因:'upload data和fileHandle是互斥的'
这表明我应该使用预定义的GTLUploadParameters工厂方法之一:
+ (GTLUploadParameters *)uploadParametersWithData:(NSData *)data
MIMEType:(NSString *)mimeType;
+ (GTLUploadParameters *)uploadParametersWithFileHandle:(NSFileHandle *)fileHandle
MIMEType:(NSString *)mimeType;
但这些都不合适(或正如我所尝试过的那样)。有没有人在iOS上使用Google云端硬盘成功创建了文件夹?
答案 0 :(得分:6)
我这样做:
GTLDriveFile *folderObj = [GTLDriveFile object];
folderObj.title = folderName;
folderObj.mimeType = @"application/vnd.google-apps.folder";
// To create a folder in a specific parent folder, specify the identifier
// of the parent:
// _resourceId is the identifier from the parent folder
if (_resourceId.length && ![_resourceId isEqualToString:@"0"]) {
GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = _resourceId;
folderObj.parents = [NSArray arrayWithObject:parentRef];
}
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folderObj uploadParameters:nil];
// executeQuery:...
答案 1 :(得分:0)
这是另一个更完整的变体:
+ (void)createFolderWithService:(GTLServiceDrive *)service
folderName:(NSString *)folderName
parentId:(NSString *)parentId
completionBlock:(void (^)(GTLServiceTicket *, GTLDriveFile *, NSError *))completionBlock {
GTLDriveFile *folderObj = [GTLDriveFile object];
folderObj.title = folderName;
folderObj.mimeType = @"application/vnd.google-apps.folder";
if (parentId.length && ![parentId isEqualToString:@"0"]) {
GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = parentId;
folderObj.parents = [NSArray arrayWithObject:parentRef];
}
GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:folderObj uploadParameters:nil];
[service executeQuery:query completionHandler:completionBlock];
}