I am very new to iOS development. I am developing an application in which I want to download a file from Google Drive (pdf, doxc, doc and xlxs) and which will be sent for sending to our server.
First I installed it in the pod file using
pod 'GoogleAPIClient/Drive', '~> 1.0.2' pod 'GTMOAuth2', '~> 1.1.0'
The second I created a project in the Google Developer Console with the package ID and got the client ID, I saved it in my project.
When user clicks file select button
-(IBAction)onclickfileFromDrive:(id)sender { self.service = [[GTLServiceDrive alloc] init]; self.service.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kClientID clientSecret:nil]; NSLog(@"service reply%hhd",self.service.authorizer.canAuthorize); if (!self.service.authorizer.canAuthorize) { [self presentViewController:[self createAuthController] animated:YES completion:nil]; } else { [self fetchFiles]; } } - (GTMOAuth2ViewControllerTouch *)createAuthController { GTMOAuth2ViewControllerTouch *authController; NSArray *scopes = [NSArray arrayWithObjects:kGTLAuthScopeDrive, nil]; authController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:[scopes componentsJoinedByString:@" "] clientID:kClientID clientSecret:nil keychainItemName:kKeychainItemName delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]; return authController; } - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error { if (error != nil) { [self showAlert:@"Authentication Error" message:error.localizedDescription]; self.service.authorizer = nil; } else { self.service.authorizer = authResult; [self dismissViewControllerAnimated:YES completion:nil]; } self.service.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kClientID clientSecret:nil]; NSLog(@"service reply%hhd",self.service.authorizer.canAuthorize); if (self.service.authorizer.canAuthorize) { [self fetchFiles]; } }
The login process is working fine. I get access token and update token
while i try to upload a file
- (void)fetchFiles { GTLQueryDrive *query =[GTLQueryDrive queryForFilesList]; query.q = @"mimeType ='application/pdf' or mimeType ='text/plain' or mimeType ='application/msword' or mimeType ='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' or mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'"; [self.service executeQuery:query delegate:self didFinishSelector:@selector(displayResultWithTicket:finishedWithObject:error:)]; } - (void)displayResultWithTicket:(GTLServiceTicket *)ticket finishedWithObject:(GTLDriveFileList *)response error:(NSError *)error { if (error == nil) { NSMutableString *filesString = [[NSMutableString alloc] init]; if (response.files.count > 0) { for (GTLDriveFile *file in response.files) { [filesString appendFormat:@"%@ (%@)\n", file.name, file.identifier]; NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?key=%@", file.identifier,kClientID]; GTMSessionFetcher *fetcher = [_service.fetcherService fetcherWithURLString:url]; [fetcher beginFetchWithCompletionHandler:^(NSData *datav, NSError *error) { if (error == nil) { NSLog(@" file web link%@",file.webViewLink); NSLog(@"file extension%@",file.fileExtension); NSLog(@"Retrieved file content%@",datav); NSString *prefixString = @"EMR File"; NSString *guid = [[NSProcessInfo processInfo] globallyUniqueString] ; NSString *uniqueFileName = [NSString stringWithFormat:@"%@_%@", prefixString, guid]; NSString *filePath = [[NSString alloc]init]; if (datav!=nil) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
file data such as
NSLog(@" file web link%@",file.webViewLink); NSLog(@"file extension%@",file.fileExtension); file.fileExtension
They get zero all the time.
However, I get a value for
file.name file.identifier
Finally, if I try to upload a file using this URL
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?key=%@", file.identifier,kClientID];
and
NSString *url = [NSString stringWithFormat:@"https://www.googleapis.com/drive/v3/files/%@?key=My API key from google console", myfile.identifier];
I get nsdata very small content
Printing description of datav:
<7b0a2022 6b696e64 223a2022 64,726,976 65,236,669 6c65222c 0a202269 64223a20 22,304,236 6a6e576c 46,497,868 434f566e 6c586546 4a496344 526d596d 68694e44 6c735432 557a516b 7057566c 70716431 5246222c 0a20226e 616d6522 3a202253 41,544,859 412e646f 6378222c 0a20226d 696d6554 79,706,522 3a202261 70706c69 63,617,469 6f6e2f76 6e642e6f 70656e78 6d6c666f 726d6174 732d6f66 66,696,365 646f6375 6d656e74 2e776f72 6470726f 63657373 696e676d 6c2e646f 63756d65 6e74220a 7d0a>
Due to getting a file extension value of zero, I tried to provide a static extension and saved it in a directory and try to view it, and I get this error
" File_B85DABFF-BCA5-4B5E-8FDC-59D2907F0509-1053-000003B0E6E4C9A9.docx" can't be opened for some reason.
I donโt know that where Iโm missing, I searched more, but could not help someone help me with this
early