【问题标题】:How download file google drive api ios如何下载文件google drive api ios
【发布时间】:2016-05-02 02:30:19
【问题描述】:

我尝试从 google drive API 下载文件 3 天后没有成功。我用过这个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);
  }
}];

所以我搜索了其他代码,但没有人解释我需要在驱动器和文件中放入什么:

  1. how to download file from google drive using objective c?(就说是网址)
  2. Google drive api download file for iOS
  3. IOS: How to Download Google Docs files using ios google drive sdk API?

解决方案:

我的示波器存在授权问题,通过完全访问驱动器解决了问题。我更改了范围(在快速入门代码中,查看:“- (GTMOAuth2ViewControllerTouch *)createAuthController...”) -->NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeDrive, nil];

供下载(受快速入门示例启发):

// 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);
            }
        }];
    }
}

如果你想保存在手机上,你可以看看巴拉的代码。

【问题讨论】:

  • 发生了哪些类型的错误?
  • 问题更多,我不知道我需要在 *drive 和 *file 中放入什么:一个标识?什么类型,什么标识?我在哪里可以找到它? (是驱动器中文件的名称?但是这段代码如何在我的google驱动器的good文件夹中找到好文件?(我不明白这个协议的功能......)

标签: ios objective-c google-drive-api


【解决方案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;
}

【讨论】:

  • 感谢您的回答。我试过了,但是当我复制并粘贴第一个块时 - (void)fetchFilelesss {这里例如}。 - 我对“for(GTLDriveFile *files.items 中的文件)”有疑问:(文件未定义)。 - 我把第二块放在哪里? GTLDriveFile *file=[driveFiles objectAtIndex:indexPath.row];
  • Hey G.Tommy ... 将 - (void)fetchFilelesss 放入 ViewDidLoad 方法中... 您将获得所有文件... 在我的代码中,我只从驱动器中获取 PDF 文件 if( [fileExtension isEqualToString:@"pdf"]) 所以我提出了这个条件......如果你想要所有文件意味着只需删除 - (void)fetchFilelesss for 循环中的条件。然后使用数组在 Tableview 中显示我的文件。当我在 didSelectRowAtIndexpath 中选择文件时,我将行 GTLDriveFile *file=[driveFiles objectAtIndex:indexPath.row];
  • 您好,经过大量研究,我想我现在更好地理解了您的代码。但是,我认为它适用于以前版本的 google drive api (v2)。我说是因为 GTMHTTPFetcher 不存在于我在官方 google drive api 上下载的库中,我认为现在我们应该使用 GTMSessionFetcher。此外,对于 file.downloadUrl :downloadUrl 不存在......现在,我可以访问我的 pdf 的 file.identifier 并且我使用 NSString *urltest = [NSString stringWithFormat:@"googleapis.com/drive/v3/files/0B-LQcMJz5Z7CUWpzekV3MnJBbWc/…;
  • 和 GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:urltest];但出现错误:发生错误:Error Domain=com.google.HTTPStatus Code=403 "(null)" UserInfo={data=mzan.com/article/… 和这个:developers.google.com/drive/v3/web/…
  • 关于错误的文档:developers.google.com/drive/v3/web/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-17
  • 2020-02-05
  • 2015-02-06
相关资源
最近更新 更多