我试图从3天开始从google drive API下载文件但没有成功。我使用了https://developers.google.com/drive/ios/devguide/files#reading_files。
但我无法理解我需要放入* drive和* file?
我试过了:
GTLDriveFile *file = @"fileText.txt";
(或者我在谷歌驱动器上尝试了我的文件的网址...)该指南没有解释......我没有找到真实的例子。
GTLServiceDrive *drive = ...;
GTLDriveFile *file = ...;
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?alt=media",
file.identifier]
GTMSessionFetcher *fetcher = [drive.fetcherService fetcherWithURLString:url];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil) {
NSLog(@"Retrieved file content");
// Do something with data
} else {
NSLog(@"An error occurred: %@", error);
}
}];
所以我搜索了其他代码,但没有人解释我需要放入驱动器和文件中的内容:
解决方案:
我的示波器存在授权问题,通过对驱动器的完全访问权限解决了问题。我改变了范围(在快速入门代码中,看看:“ - (GTMOAuth2ViewControllerTouch *)createAuthController ......”)
- > NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeDrive, nil];
下载(受quickstart示例启发):
// self.service is my GTLServiceDrive
// When the view appears, ensure that the Drive API service is authorized, and perform API calls.
- (void)viewDidAppear:(BOOL)animated {
if (!self.service.authorizer.canAuthorize) {
// Not yet authorized, request authorization by pushing the login UI onto the UI stack.
[self presentViewController:[self createAuthController] animated:YES completion:nil];
} else {
NSString *urltest = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?alt=media", identifier_file]; //the ID of my file in a string identifier_file
GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:urltest]; // the request
// receive response and play it in web view:
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *errorrr) {
if (errorrr == nil) {
NSLog(@"Retrieved file content");
[webView_screen loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil]; //my file is a pdf
[webView_screen reload];
} else {
NSLog(@"An error occurred: %@", errorrr);
}
}];
如果您想保存在手机上,可以查看Bala的代码。
答案 0 :(得分:1)
首先从云端硬盘
获取文件driveFiles = [[NSMutableArray alloc] init];
for (GTLDriveFile *file in files.items) {
if ([file.mimeType isEqualToString:@"application/vnd.google-apps.folder"])
{
}
else
{
NSString *fileExtension=file.fileExtension;
if (fileExtension)
{
if([fileExtension isEqualToString:@"pdf"])
{
[driveFiles addObject:file];
}
}
}
}
GTLDriveFile会传递数组中的对象
GTLDriveFile *file=[driveFiles objectAtIndex:indexPath.row];
这是下载文件的代码
NSString *link;
if (file.webContentLink){
link=file.webContentLink;
}
else if(file.embedLink){
link=file.embedLink;
}
else
{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"ERROR" message:@"File has no downloadable link" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
if (link) {
NSString *downloadUrl = file.downloadUrl;
GTMHTTPFetcher *fetcher = [self.driveService.fetcherService fetcherWithURLString:downloadUrl];
//async call to download the file data
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
if (error == nil) {
if ( data ){
NSString *dirPath = [self directoryPathForSavingFile];
NSString *filePath = [dirPath stringByAppendingPathComponent:file.title];
[self saveFileJSONData:data forFileName:filePath withCompletionHandler:^(BOOL successStatus) {
// Adding skip attribute to avoid data sinking in iCloud
BOOL path = [[NSFileManager defaultManager] fileExistsAtPath:filePath];
if (path) {
NSLog(@"filePath %@",filePath);
}
}];
}
} else {
NSLog(@"An error occurred: %@", error);
}
}];
}
保存文件的目录路径代码
-(NSString*)directoryPathForSavingFile:(NSString *)directoryName
{
NSString *applicationDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
applicationDirectory = [applicationDirectory stringByAppendingPathComponent:directoryName];
return applicationDirectory;
}