IOS:如何使用IOS Google drive sdk API下载Google文档文件?
我将google drivs sdk与我的ios应用程序集成。目前,我使用以下代码从我的谷歌硬盘a/c下载基于下载url链接的文件。但是,当我试图下载google文档文件(其mime类型为application/vnd.google apps.document)时,google drive库中没有下载url链接。在这种情况下,我如何下载谷歌文档数据?。我可以使用alternateLink代替下载url链接吗?。任何帮助都必须感谢 我的代码:IOS:如何使用IOS Google drive sdk API下载Google文档文件?,ios,google-drive-api,google-api-objc-client,Ios,Google Drive Api,Google Api Objc Client,我将google drivs sdk与我的ios应用程序集成。目前,我使用以下代码从我的谷歌硬盘a/c下载基于下载url链接的文件。但是,当我试图下载google文档文件(其mime类型为application/vnd.google apps.document)时,google drive库中没有下载url链接。在这种情况下,我如何下载谷歌文档数据?。我可以使用alternateLink代替下载url链接吗?。任何帮助都必须感谢 我的代码: - (void)loadFileContent {
- (void)loadFileContent {
GTMHTTPFetcher *fetcher =
[self.driveService.fetcherService fetcherWithURLString:[[self.driveFiles objectAtIndex:selectedFileIdx] downloadUrl]];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil) {
NSLog(@"\nfile %@ downloaded successfully from google drive", [[self.driveFiles objectAtIndex:selectedFileIdx] originalFilename]);
//saving the downloaded data into temporary location
} else {
NSLog(@"An error occurred: %@", error);
}
}];
}谷歌文档本机格式的文档不能作为其他文件下载,只能使用
exportLinks
URL导出为不同的支持格式
有关更多详细信息和支持的格式列表,请查看Google Drive SDK文档:
我以前也有过类似的问题。具体来说,我没有找到在Google文档中创建的本机文档的下载URL。它们是空的。只有通过DrEdit(驱动器SDK中举例说明的解决方案)创建的内容与下载URL关联 该解决方案实际上嵌入在GTLDriveFileExportLinks实例中的属性:JSON中,该实例返回NSMutableDictionary*。您可以选择通过access JSONString属性查看JSON对象的内容。可以通过查询GTLDriveFile实例中的exportLinks来获取JSON可变字典。示例如下:
GTLDriveFile *file = files.items[0]; // assume file is assigned to a valid instance.
NSMutableDictionary *jsonDict = file.exportLinks.JSON;
NSLog(@"URL:%@.", [jsonDict objectForKey:@"text/plain"]);
下面是从google drive下载文件的步骤。它既适用于文件,也适用于谷歌文档 步骤1: 获取文件列表并使用相关文件下载链接url将其存储到数组或dict中:
- (void)loadDriveFiles {
fileFetchStatusFailure = NO;
//for more info about fetching the files check this link
//https://developers.google.com/drive/v2/reference/children/list
GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:[parentIdList lastObject]];
query2.maxResults = 1000;
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query2
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveChildList *children, NSError *error) {
GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];
//incase there is no files under this folder then we can avoid the fetching process
if (!children.items.count) {
[self.driveFiles removeAllObjects];
[fileNames removeAllObjects];
[self performSelectorOnMainThread:@selector(reloadTableDataFromMainThread) withObject:nil waitUntilDone:NO];
return ;
}
if (error == nil) {
int totalChildren = children.items.count;
count = 0;
[self.driveFiles removeAllObjects];
[fileNames removeAllObjects]; //http://stackoverflow.com/questions/14603432/listing-all-files-from-specified-folders-in-google-drive-through-ios-google-driv/14610713#14610713
for (GTLDriveChildReference *child in children) {
GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];
query.completionBlock = ^(GTLServiceTicket *ticket, GTLDriveFile *file, NSError *error) {
//increment count inside this call is very important. Becasue the execute query call is asynchronous
count ++;
NSLog(@"Google Drive: retrieving children info: %d", count);
if (error == nil) {
if (file != nil) { //checking the file resource is available or not
//only add the file info if that file was not in trash
if (file.labels.trashed.intValue != 1 )
[self addFileMetaDataInfo:file numberOfChilderns:totalChildren];
}
//the process passed all the files then we need to sort the retrieved files
if (count == totalChildren) {
NSLog(@"Google Drive: processed all children, now stopping HUDView - 1");
[self performSelectorOnMainThread:@selector(reloadTableDataFromMainThread) withObject:nil waitUntilDone:NO];
}
} else {
//the file resource was not found
NSLog(@"Google Drive: error occurred while retrieving file info: %@", error);
if (count == totalChildren) {
NSLog(@"Google Drive: processed all children, now stopping HUDView - 2");
[self performSelectorOnMainThread:@selector(reloadTableDataFromMainThread)
withObject:nil waitUntilDone:NO];
}
}
};
//add the query into batch query. Since we no need to iterate the google server for each child.
[batchQuery addQuery:query];
}
//finally execute the batch query. Since the file retrieve process is much faster because it will get all file metadata info at once
[self.driveService executeQuery:batchQuery
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *file,
NSError *error) {
}];
NSLog(@"\nGoogle Drive: file count in the folder: %d", children.items.count);
} else {
NSLog(@"Google Drive: error occurred while retrieving children list from parent folder: %@", error);
}
}];
}
步骤2:
添加文件元数据信息
-(void)addFileMetaDataInfo:(GTLDriveFile*)file numberOfChilderns:(int)totalChildren
{
NSString *fileName = @"";
NSString *downloadURL = @"";
BOOL isFolder = NO;
if (file.originalFilename.length)
fileName = file.originalFilename;
else
fileName = file.title;
if ([file.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
isFolder = YES;
} else {
//the file download url not exists for native google docs. Sicne we can set the import file mime type
//here we set the mime as pdf. Since we can download the file content in the form of pdf
if (!file.downloadUrl) {
GTLDriveFileExportLinks *fileExportLinks;
NSString *exportFormat = @"application/pdf";
fileExportLinks = [file exportLinks];
downloadURL = [fileExportLinks JSONValueForKey:exportFormat];
} else {
downloadURL = file.downloadUrl;
}
}
if (![fileNames containsObject:fileName]) {
[fileNames addObject:fileName];
NSArray *fileInfoArray = [NSArray arrayWithObjects:file.identifier, file.mimeType, downloadURL,
[NSNumber numberWithBool:isFolder], nil];
NSDictionary *dict = [NSDictionary dictionaryWithObject:fileInfoArray forKey:fileName];
[self.driveFiles addObject:dict];
}
}
步骤3:
根据表格行上的文件选择下载文件
NSString *downloadUrl = [[[[self.driveFiles objectAtIndex:selectedFileIdx] allValues] objectAtIndex:0]
objectAtIndex:download_url_link];
NSLog(@"\n\ngoogle drive file download url link = %@", downloadUrl);
GTMHTTPFetcher *fetcher =
[self.driveService.fetcherService fetcherWithURLString:downloadUrl];
//async call to download the file data
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil) {
NSLog(@"\nfile %@ downloaded successfully from google drive", self.selectedFileName);
//saving the downloaded data into temporary location
[data writeToFile:<path> atomically:YES];
} else {
NSLog(@"An error occurred: %@", error);
}
}];
NSString*downloadUrl=[[[self.driveFiles objectAtIndex:selectedFileIdx]allValues]objectAtIndex:0]
objectAtIndex:下载\u url\u链接];
NSLog(@“\n\n日志驱动器文件下载url链接=%@”,下载url);
GTMHTTPFetcher*取数器=
[self.driveService.fetcherService fetcherWithURLString:downloadUrl];
//异步调用以下载文件数据
[fetcher beginFetchWithCompletionHandler:^(NSData*数据,NSError*错误){
如果(错误==nil){
NSLog(@“\n文件%@已从google drive成功下载”,self.selectedFileName);
//将下载的数据保存到临时位置
[数据写入文件:原子:是];
}否则{
NSLog(@“发生错误:%@”,错误);
}
}];
我查看了文档。但是在那里,我找不到任何与Objectice-C相关的示例。因此,您能告诉我如何从GTLDriveFileExportLinks类中获取导出类型应用程序/pdf。的Objective-C示例演示了如何下载或导出文件您是否设法下载了这些文件?我想知道你是怎么做到的。我也在尝试下载这些文件,但到目前为止,我找不到任何可以帮助我的资源。@Shailesh,是的,我可以从google drive下载这些文件。在我的例子中,当用户点击文件名时,我启动了下载操作。你面临的问题是什么?我使用的是谷歌提供的一个样本代码。项目名称为“DriveSample”,我得到的导出/下载URL为空。下载完全失败。这是我用作参考的URL--@Shailesh,我刚刚在下面发布了我的代码,你可以查看。通常,在获取文件列表信息的同时,谷歌也会为您提供下载url链接,这与本机谷歌文档和您的附件不同。当您知道下载url链接后,您可以使用该url启动下载操作。谢谢!成功了。:)虽然我觉得,谷歌需要开发他们的API文档。在我们的项目中,它的实现方式很复杂。另一方面,Dropbox和SkyDrive有简单而直接的过程。@Shailesh,太棒了!!!:-)是的,你是对的:-)当我开始集成谷歌硬盘时,我遇到了很多问题。@loganathan如果数据超过1GB,这个应用程序会崩溃吗?@MatrosovAlexander,我不确定。我没有检查此用户案例路径。请帮助mi如何在UI中下载按钮单击