iosobjective-crestgoogle-drive-api

Google Drive REST in iOS


My question is rather simple. After integrating Google Drive REST API I could finally download files from my Google Drive.SO I have followed this example https://developers.google.com/drive/v3/web/manage-downloads Everything is working fine,and in console I get the information that the file is downloaded. So here is the question:where it goes,to which folder? What is the path to where the file is saved? And how do I set the necessary save path if I need to save a file to iOS Documents folder?

NSString *fileId = @"0BwwA4oUTeiV1UVNwOHItT0xfa2M";

GTLRQuery *query = [GTLRDriveQuery_FilesGet queryForMediaWithFileId:fileId];
[driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDataObject *file,
                                                     NSError *error) {
    if (error == nil) {
        NSLog(@"Downloaded %lu bytes", file.data.length);
    } else {
        NSLog(@"An error occurred: %@", error);
    }
}];

I am developing for iOS 10 and an above and as I read the information:NOTE: Because NSURLConnection is deprecated as of iOS 9 and OS X 10.11, this class has been superseded by GTMSessionFetcher. I should use either code provided by Google (posted above) or GTMSessionFetcher. I am using code from Google, but Will be thankful if someone will help me solve my problem with both (Google and GTMSessionFetcher) variants.


Solution

  • UPD: For Google API Client lib

    GTLRDataObject has data property which contains raw bytes of your file. It is enough to safe your file using standard iOS methods.

    There is also contentType property which is a string with MIME type of the file. You probably want to store this information somewhere along with the path to saved data, it will help you to properly display what kind of file (image/song/text) it is, as well as use it when you decode/open/read saved data to present actual information.

    GTLRQuery *query = [GTLRDriveQuery_FilesGet queryForMediaWithFileId:fileId];
    [driveService executeQuery:query completionHandler:^(GTLRServiceTicket *ticket,
                                                     GTLRDataObject *file,
                                                     NSError *error) {
        if (error == nil) {
            NSLog(@"Downloaded %lu bytes", file.data.length);
    
            // Here is where you take these bytes and save them as file to any location you want (typically your Documents folder)
            NSURL *documentsDirectoryURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
            [file.data writeToURL:documentsDirectoryURL atomically:YES];
    
            // Now you can store the url/path of the saved file somewhere along with the MIME type string (maybe new class or structure describing the file in your app)
            MyFile *file = [[MyFile alloc] initWithFilePath:[documentsDirectoryURL path] mimeType:contentType];
        } else {
            NSLog(@"An error occurred: %@", error);
        }
    }];
    

    See CocoaDocs for GTLRDataObject reference.


    Generic version: using iOS SDK

    If you use NSURLSession, it has delegate method which you use to move file from temporary location to anywhere you need.

    Obj-C

    - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
        // Use `location` to move your data to Documents directory or wherever else
    }
    

    Swift

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        // Use `location` to move your data to Documents directory or wherever else
    }