4
votes

I am working with download files from google drive. But I am facing lots of issues using google SDK. In developer.google.com there is some examples are available.

 GTLServiceDrive *drive = ...;

    GTLDriveFile *file = ...;

    GTMHTTPFetcher *fetcher = [drive.fetcherService fetcherWithURLString:file.downloadUrl];`

    `[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
      if (error == nil) {
        NSLog(@"Retrieved file content");
        // Do something with data
      } else {
        NSLog(@"An error occurred: %@", error);    
      }
    }];

I have downloaded new sdks from developer.google.com site, but in above example given GTMHTTPFetcher *fetcher = [drive.fetcherService fetcherWithURLString:file.downloadUrl];. But in new SDK,in this GTLDriveFile there is no downloadURL object.

And in another example it is asking for client secret key, but when I am selecting type of iOS then there is no option for client secret key. But when I am choosing web that time it is showing. but it is not working.

Please help me how to download file from google drive in iOS using objective c.

2
According to the documentation, there is a downloadURL object. Please double check if you are using the right resource. See downloadURLgerardnimo
Hi gerardnimo, I have downloaded new SDK from doveloper.google.com site. I have checked double time, that there is not available downloadURL object in GTLDriveFile. In the documentation there is available, but I think its old documentation. please help me resolve this issue. I am trying last 5 days. still not got any solution.Asha Sharma

2 Answers

1
votes

This may Help you you need to add key (API keys for iOS) param in URL

GTLDriveFile *myfile;//Your File Object
    NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?key=YOUR_KEY_iOS",
                     myfile.identifier];
    GTMSessionFetcher *fetcher = [self.service.fetcherService fetcherWithURLString:url]; //GTLServiceDrive *service;

    [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
        if (error == nil) {
            NSLog(@"Retrieved file content");
            // File Downloaded!
        } else {
            NSLog(@"An error occurred: %@", error);
        }
    }];
0
votes

From google documentation:

https://developers.google.com/drive/v2/reference/files/get

+ (void)downloadFileContentWithService:(GTLServiceDrive *)service
                                  file:(GTLDriveFile *)file
                       completionBlock:(void (^)(NSData *, NSError *))completionBlock {
  if (file.downloadUrl != nil) {
    // More information about GTMHTTPFetcher can be found on
    // <a href="http://code.google.com/p/gtm-http-fetcher">http://code.google.com/p/gtm-http-fetcher</a>
    GTMHTTPFetcher *fetcher =
      [service.fetcherService fetcherWithURLString:file.downloadUrl];

    [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
      if (error == nil) {
        // Success.
        completionBlock(data, nil);
      } else {
        NSLog(@"An error occurred: %@", error);
        completionBlock(nil, error);
      }
    }];
  } else {
    completionBlock(nil,
                    [NSError errorWithDomain:NSURLErrorDomain
                                        code:NSURLErrorBadUrl
                                    userInfo:nil]);
  }
}

It may be old documentation, so check out this answer:

Value of type 'GTLDriveFile' has no member 'downloadUrl'