SwiftyDropbox list folders only

1.1k Views Asked by At

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?

2

There are 2 best solutions below

1
On BEST ANSWER

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)
    }
}
0
On

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.