Using the following method, I am able to get the sub-folders, but this code retrieves all the folders from the root. For example, if there is a folder in trash also it retrieves that along with folders in My Drive.
My requirement is that, if the folder is not there in the My Drive, then first I should create one and then insert files. My problem is, when I am checking for the folder name, as the folder exist in trash but not in My Drive, I am getting response as folder exists.
Here is my method to retrieve folders.
If anyone has some idea, please let me know.
-(void)getFileListFromSpecifiedParentFolder {
GTLQueryDrive *query2 = [GTLQueryDrive queryForFilesList];
// GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:#"My Drive"];
query2.q = #"";
//or i also use this code
query2.q = #"mimeType = 'application/vnd.google-apps.folder'";
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query2
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveChildList *children, NSError *error) {
NSLog(#"\nGoogle Drive: file count in the folder: %d", children.items.count);
//incase there is no files under this folder then we can avoid the fetching process
if (!children.items.count) {
return ;
}
if (error == nil) {
for (GTLDriveChildReference *child in children) {
GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *file,
NSError *error) {
NSLog(#"\nfile name = %# \n file kind %# \n file identifier %#", file.title,file.kind,file.identifier);
}];
}
}
else
[self createFolderForGoogleDriveWithName:#"Music"];
}];
}
You can filter out trashed directories with the following query: application/vnd.google-apps.folder' and trashed = false
Related
After looking and finding for a shared folder that matches a given name, I need to know if that folder's creator/owner is a specific google account. How could I achieve that utilizing Google Drive's Objective-C SDK.
Here is my query that finds the shared folder given its name:
GTLQueryDrive *queryDrive = [GTLQueryDrive queryForFilesList];
queryDrive.q = #"mimeType='application/vnd.google-apps.folder' and trashed=false";
[self.service executeQuery:queryDrive completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *files,
NSError *error) {
if (error == nil)
{
NSString *identifier = #"";
for(GTLDriveFile *file in files.files)
{
NSArray *tempArray = [NSArray arrayWithArray:file.owners];
NSLog(#"File Title: %# | ID: %#",file.name , file.identifier);
if ([file.name isEqualToString:#"GIVEN FOLDER NAME"]) {
identifier = file.identifier;
//How to know if the found folder is owned by a specified email?
break;
}
}
if ([identifier isEqualToString:#""]) {
[UISpinningWheelController removeSpinningWheel:self.view];
[self showAlertWithTitle:#"Error" andMessage:#"The required folder has not been shared with this Google account. Please contact the administrator."];
}
}
else {
[UISpinningWheelController removeSpinningWheel:self.view];
[self showAlertWithTitle:#"Error" andMessage:[error localizedDescription]];
}
}];
Thank you so much for your help.
I'm trying to integrate Google Drive API with my iOS application. I'm unable to download files, however, and I think it has to do with the MIME type. In the API console I set the MIME types like so:
And I download files with the following code:
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = #"mimeType = 'text/plain'";
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *files,
NSError *error) {
if (error == nil) {
if (self.googleDriveFiles == nil) {
self.googleDriveFiles = [[NSMutableArray alloc] init];
}
[self.googleDriveFiles removeAllObjects];
[self.googleDriveFiles addObjectsFromArray:files.items];
NSLog(#"files: %#",self.googleDriveFiles);
[self.noteTableview reloadData];
} else {
NSLog(#"An error occurred: %#", error);
//[DrEditUtilities showErrorMessageWithTitle:#"Unable to load files"
// message:[error description]
// delegate:self];
}
}];
NSLog outputs (null) What's wrong here?
Using the Google Drive iOS API, how can I create a folder AND share it?
GTLDriveFile * folder = [GTLDriveFile object];
folder.title = #"Test";
folder.mimeType = #"application/vnd.google-apps.folder";
// ???
May be this one helps you. This code is for creating folder with specific name.
Code 1 :
-(void)createFolderWithName:(NSString *)folderName {
GTLDriveFile *file = [GTLDriveFile object];
file.title = #"FileUpload";
file.mimeType = #"application/vnd.google-apps.folder";
// To create a folder in a specific parent folder, specify the identifier
// of the parent:
// _resourceId is the identifier from the parent folder. Here i am using root as the parent folder.
NSString *resourceId = #"root";
if (resourceId.length && ![resourceId isEqualToString:#"0"]) {
GTLDriveParentReference *parentRef = [GTLDriveParentReference object];
parentRef.identifier = resourceId;
file.parents = [NSArray arrayWithObject:parentRef];
}
GTLQueryDrive *query1 = [GTLQueryDrive queryForFilesInsertWithObject:file uploadParameters:nil];
[[self appDelegate].driveService executeQuery:query1 completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (!error) {
//I see this in the console...
//No Errors in Folder Query:
//GTLDriveFileList 0x108f3430: {kind:"drive#fileList"
//etag:""Q5ElJByAJoL0etObruYVPRipH1k/vyGp6PvFo4RvsFtPoIWeCReyIC8""}
NSLog(#"No Errors in Folder Query: %#",object);
GTLDriveFileList *list = (GTLDriveFileList *)object;
//So, none of this for() loop happens
for (GTLDriveFile *file in list.items) {
NSLog(#"Folder: \n \n %# \n \n",file);
//NSLog(#"\n file extension value is %#",file.fileExtension);
//NSLog(#"\n file size value is %#",file.fileSize);
}
} else {
NSLog(#"Folder Query Error: %#",error);
}
}];
}
In the above code resourceId is the parent folder id, under which u want to create the folder.
I don't know how to share the full folder, but i think it is same like sharing the file.
If u want to know about particular file sharing please refer this link. In the link you can find the example for objective-c code.
https://developers.google.com/drive/v2/reference/permissions/insert
Actually i integrated google drive sdk with my ios app. I can able to retrieve/upload the files in google drive through google drive ios sdk. But the retrieving files list from specified parent folder taking so much time.
Here the steps and the code which was i am using.
First am getting the children's of the specified parent folder then am getting the GTLDriveChildReference of each child then
then am querying with the child reference identifier.
This is huge process for me. Also it request google server each time. Any better way there to just pass the parent folder id in a query and pulling the files from that parent folder.
-(void)getFileListFromSpecifiedParentFolder {
GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:<some_id or root>];
query2.maxResults = 1000;
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query2
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveChildList *children, NSError *error) {
NSLog(#"\nGoogle Drive: file count in the folder: %d", children.items.count);
//incase there is no files under this folder then we can avoid the fetching process
if (!children.items.count) {
return ;
}
if (error == nil) {
for (GTLDriveChildReference *child in children) {
GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *file,
NSError *error) {
NSLog(#"\nfile name = %#", file.originalFilename);
}];
}
}
}];
}
Any help that might be appreciated.
I've been using the following code (which only requires one query rather than multiple queries):
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = [NSString stringWithFormat:#"'%#' IN parents", <the folderID>];
[self.driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *files,
NSError *error) {
...
}];
Seems to be working well thus far.
Try combining queries in a batch query, like
GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];
for (GTLDriveChildReference *child in children) {
GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];
query.completionBlock = ^(GTLServiceTicket *ticket, GTLDriveFile *file, NSError *error) {
NSLog(#"error=%#, file name = %#", error, file.title);
};
[batchQuery addQuery:query];
}
[driveService executeQuery:batchQuery completionHandler:...]
If automatic fetching of result pages is enabled (service.shouldFetchNextPages = YES) then try to set the maxResults property of queries to avoid the need for the library to make multiple fetches. For example, if there are 100 or fewer results for a query, a maxResults value of 100 will retrieve them with a single fetch.
Requests for partial responses are also a bit faster and use much less memory, particularly for large results.
Look at http logs for a better idea of the response sizes for your queries.
Am developing ios app including google drive. In google drive when am trying to list files, it displays all folders, subfolders, subfiles in single page itself. These is my code
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
[driveService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *filesList,
NSError *error) {
if (error == nil) {
[FilesFromGoogleDrive addObjectsFromArray:filesList.items
];
};
I dont want to list all files in main page. I need similar to google drive application for accessing folders, subfolders, subfiles in sequential way.Am trying this from past one week but there is no good result. So please help me how to access folders, subfolders.
If you want to list all files in a folder identified by folderId, you can use the following code (from https://developers.google.com/drive/v2/reference/children/list):
+ (void)printFilesInFolderWithService:(GTLServiceDrive *)service
folderId:(NSString *)folderId {
// The service can be set to automatically fetch all pages of the result. More
// information can be found on https://code.google.com/p/google-api-objectivec-client/wiki/Introduction#Result_Pages.
service.shouldFetchNextPages = YES;
GTLQueryDrive *query =
[GTLQueryDrive queryForChildrenListWithFolderId:folderId];
// queryTicket can be used to track the status of the request.
GTLServiceTicket *queryTicket =
[service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveChildList *children, NSError *error) {
if (error == nil) {
for (GTLDriveChildReference *child in children) {
NSLog(#"File Id: %#", child.identifier);
}
} else {
NSLog(#"An error occurred: %#", error);
}
}];
}
Another option is to search for files having folderId in their Parents collection:
https://developers.google.com/drive/search-parameters
For instance, you can have a search query like the following:
'1234567' in parents
Where '1234567' is the id of the folder you want to list files for.
I hope this is one that you want. Just use this code this one will give you what do you need.
-(void)getFileListFromSpecifiedParentFolder {
GTLQueryDrive *query2 = [GTLQueryDrive queryForChildrenListWithFolderId:<some_folder_id or root>];
query2.maxResults = 1000;
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query2
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveChildList *children, NSError *error) {
NSLog(#"\nGoogle Drive: file count in the folder: %d", children.items.count);
//incase there is no files under this folder then we can avoid the fetching process
if (!children.items.count) {
return ;
}
if (error == nil) {
for (GTLDriveChildReference *child in children) {
GTLQuery *query = [GTLQueryDrive queryForFilesGetWithFileId:child.identifier];
// queryTicket can be used to track the status of the request.
[self.driveService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFile *file,
NSError *error) {
NSLog(#"\nfile name = %#", file.originalFilename);
}];
}
}
}];
}
Here is how I did it:
NSString* currentFolderID = #"root";//folder id for the Google Drive root directory
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
NSString* queryQ = [NSString stringWithFormat: #"trashed = false and '%#' in parents", currentFolderID];
query.q = queryQ;
[_googleService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *files,
NSError *error)
{
if (error == nil)
{
[self listFiles: files.items];
}
Use #"root" as the parent to list files from the root directory, and use file.identifier of the subfolder item as the parent to list files from. To query files from a subfolder, use the following 2 lines to get the parent identifier for all files in the subfolder.
GTLDriveFile subFolderItem;
currentFolderID = subFolderItem.identifier;
To fetch the list of all files of Google Drive, use below code:
GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
query.q = [NSString stringWithFormat:#"'%#' IN parents", #"root"];
[self.serviceDrive executeQuery:query completionHandler:^(GTLServiceTicket *ticket,
GTLDriveFileList *files,
NSError *error) {
if (error == nil)
{
NSMutableArray * driveFiles = [[NSMutableArray alloc] init];
[driveFiles addObjectsFromArray:files.items];
for (GTLDriveFile *file in driveFiles)
NSLog(#"File is %#", file.title);
}
else
{
NSLog(#"An error occurred: %#", error);
}
}];