使用Google Drive SDK iOS创build文件夹
我正在尝试使用Google Drive SDK for iOS创build一个文件夹。 从这里的Google云端硬盘文档:
https://developers.google.com/drive/folder
它说,创build一个文件夹就像创build一个特殊的MIMEtypes的“文件”。
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',原因:'上传数据和fileHandle是互斥的'
这表明我应该使用预定义的GTLUploadParameters工厂方法之一:
+ (GTLUploadParameters *)uploadParametersWithData:(NSData *)data MIMEType:(NSString *)mimeType; + (GTLUploadParameters *)uploadParametersWithFileHandle:(NSFileHandle *)fileHandle MIMEType:(NSString *)mimeType;
但是这些都不合适(或者工作,因为我已经尝试了)。 有没有人在iOS上使用Google云端硬盘成功创build文件夹?
我这样做:
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:...
这是另一个更完整的变体:
+ (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]; }