2012-12-03 81 views
3

我正嘗試使用適用於iOS的Google Drive SDK創建文件夾。從谷歌驅動器文檔瀏覽:使用Google Drive SDK創建文件夾iOS

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」,理由是:「上傳數據和文件句柄是相互排斥的」

這表明我應該使用預定義的一個GTLUploadParameters工廠方法:

+ (GTLUploadParameters *)uploadParametersWithData:(NSData *)data 
             MIMEType:(NSString *)mimeType; 

+ (GTLUploadParameters *)uploadParametersWithFileHandle:(NSFileHandle *)fileHandle 
               MIMEType:(NSString *)mimeType; 

但這些都不合適(或工作,因爲我已經嘗試過)。有沒有人在iOS上使用Google雲端硬盤成功創建文件夾?

回答

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:... 
+0

是這方面的工作 –

+0

@rmaddy,你可以分享你的completionBlock?是否傳遞了相同的參數,例如一個'GTLDriveFile *'? – tarabyte

+0

@tarabyte什麼完成塊? – rmaddy

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]; 
} 
相關問題