Search code examples
iosprogressdrive

How can I get the upload progress?


I use Google official documentation for the Drive to upload files, but don't know how to upload progress to obtain.The official document said

Listening to the download progress

enter image description here


Solution

  • Using this to upload a file to drive and listen progress:

    GTLServiceDrive *driveService = ...;
    NSString *title = @"hello.txt";
    NSString *content = @"Hello world";
    NSString *mimeType = @"text/plain";
    
    GTLDriveFile *metadata = [GTLDriveFile object];
    metadata.title = title;
    
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    GTLUploadParameters *uploadParameters = [GTLUploadParameters uploadParametersWithData:data MIMEType:mimeType];
    GTLQueryDrive *query = [GTLQueryDrive queryForFilesInsertWithObject:metadata
                                                       uploadParameters:uploadParameters];
    GTLServiceTicket *uploadTicket = [driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
                                                         GTLDriveFile *updatedFile,
                                                         NSError *error) {
        if (error == nil) {
            NSLog(@"File %@", updatedFile);
        } else {
            NSLog(@"An error occurred: %@", error);
        }
    }];
    
    
    [uploadTicket setUploadProgressBlock:^(GTLServiceTicket *ticket, unsigned long long totalBytesWritten, unsigned long long totalBytesExpectedToWrite)
     {
         // progress here
     }];