Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftyDropbox list folders only

Im using SwiftyDropbox SDK in my iOS application, im trying to list folders only in my app then user can choose a folder (not a file).

in ViewController => viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    guard let dropboxClient = DropboxClientsManager.authorizedClient else{
        return
    }

    let listFolders = dropboxClient.files.listFolder(path: "")
    listFolders.response{ response, error in
        guard let result = response else{
            return
        }

        for entry in result.entries{
            print(entry)
        }
    }
    // Do any additional setup after loading the view, typically from a nib.
}

entry is >

  { 
      id = "id:0GMPvYwuVEAAAAAAAAAABw";
      name = "Folder A";
      "path_display" = "/Folder A";
      "path_lower" = "/folder a";
  }

how can i find this entry is folder and it contains sub folder or not?

like image 657
Color Players Avatar asked Jan 03 '23 16:01

Color Players


2 Answers

You can cast each entry inside of your result.entries for loop like this

override func viewDidLoad() {
    super.viewDidLoad()

    guard let dropboxClient = DropboxClientsManager.authorizedClient else{
        return
    }

    for entry in result.entries{
       guard let file = entry as? Files.FolderMetadata else{
         return
       }

       // only folders
       print(entry)

       // *********  or 
       gurad let entry is Files.FolderMetadata else{
         return
       }

       // only folders
       print(entry)
    }
}
like image 123
Kamaal ABOOTHALIB Avatar answered Jan 06 '23 05:01

Kamaal ABOOTHALIB


The Dropbox API doesn't offer a way to list folders only (though we'll consider it a feature request), so you'll need to list everything and filter out files.

You can distinguish between FileMetadata, FolderMetadata, and DeletedMetadata by switching on the Metadata object as shown in the README.

If you need subfolders as well, you can specify recursive=true when calling listFolder.

like image 26
Greg Avatar answered Jan 06 '23 05:01

Greg