Search code examples
iosswiftswift3google-drive-apigoogle-signin

Getting a list of folders in base directory for Google Drive iOS in Swift 3?


Using GoogleSignIn and GoogleAPIClientForREST, I've been able to sign in to google and even get a list of files just fine using code taken from the Google Drive iOS quickstart. Really what I don't understand is how to filter out results from the request by file type, or really how to get contents of a subfolder. Google Drive REST documentation hasn't helped at all.

To clarify the issue: while

let query = GTLRDriveQuery_FilesList.query()

query.pageSize = 10

service.executeQuery(query,
                             delegate: self,
                             didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:)))

works perfectly with

func displayResultWithTicket(ticket: GTLRServiceTicket,
                             finishedWithObject result : GTLRDrive_FileList,
                             error : NSError?) {

    if let error = error {
        print(error.localizedDescription)
        return
    }

    var text = "";

    if let files = result.files, !files.isEmpty {

        text += "Files:\n"
        for file in files {
            text += "\(file.name!) (\(file.identifier!))\n"
        }
    } else {
        text += "No files found."
    }
    print(text)
}

, I have no idea how to get only the folders or how to get files in a subfolder.


Solution

  • For Listing Folders we want to Declare the mimeType Just Like this

      let root = "(mimeType = 'application/vnd.google-apps.folder' or mimeType = 'image/jpeg' or mimeType = 'image/png' or mimeType = 'application/pdf' or mimeType = 'application/epub' or mimeType = 'application/msword' or mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') and '\(root)' in parents and trashed=false"
    

    Now you can pass this root for list only your required needs

    func GoogleDrivelistFiles(root : String) { //root 
        let query = GTLRDriveQuery_FilesList.query()
        query.pageSize = 100
        query.q = root
        query.fields = "files(id,name,mimeType,modifiedTime,createdTime,fileExtension,size,parents,kind),nextPageToken"
        service.executeQuery(query, completionHandler: {(ticket, files, error) in
            if let filesList : GTLRDrive_FileList = files as? GTLRDrive_FileList {
    
                var cloudArray = Array<FileObject>()
                if let filesShow : [GTLRDrive_File] = filesList.files {
                    for Array in filesShow {
                        let name = Array.name
                        let mimeType = Array.mimeType
                        let id = Array.identifier
                        let folder = (mimeType as NSString?)?.pathExtension
                        let isfolder = true
                        let parents = Array.parents
                        var parentPath : String!
                        for arrayParents in parents! {
                            parentPath = arrayParents
                        }
    
                        let creationTime = Array.createdTime?.date
                        let modifiedTime = Array.modifiedTime?.date
                        let fileSize : Int!
                        let fileExtension : String!
                        if isfolder == true {
                            fileSize = 0
                            fileExtension = "Folder"
                        }else {
                            fileSize = Array.size?.intValue
                            fileExtension = Array.fileExtension
                        }
                        if folder == "folder" {
                        let content = FileObject(path: mimeType!, isFolder: true, parentPath: parentPath!, fileName: name!, creationDate: creationTime!, lastModified: modifiedTime!, fileSize: fileSize!, fileExtension: fileExtension!, fileId: id!, isSyncing: true)
                        cloudArray.append(content)
                        } else {
                            let content = FileObject(path: mimeType!, isFolder: false, parentPath: parentPath!, fileName: name!, creationDate: creationTime!, lastModified: modifiedTime!, fileSize: fileSize!, fileExtension: fileExtension!, fileId: id!, isSyncing: true)
                            cloudArray.append(content)
                        }
                    }
                }
                DispatchQueue.main.async {
                self.delegate?.DataRefresh(fileObject: cloudArray)
                }
    
            }else {
    
            }
        })
    }
    

    For getting the subFolder also you can write on didSelectTable and you want to pass this root to the same func GoogleDriveListFiles().

    //ForGeting SubFolders//
     func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let FolderId = array[indexpath.row].id
     //important step
     let FolderIdroot = "(mimeType = 'application/vnd.google-apps.folder' or mimeType = 'image/jpeg' or mimeType = 'image/png' or mimeType = 'application/pdf' or mimeType = 'application/epub' or mimeType = 'application/msword' or mimeType = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') and '\(FolderId)' in parents and trashed=false"
    
      //now you can call that fun which I have already written for listingFiles func GoogleDrivelistFiles(root :String)//
    
       GoogleDrivelistFiles(root : FolderIdRoot)
     }