How To Exchange Data Between Swift and Dropbox
In this article, we will cover how to connect your own Swift project to your Dropbox account
Sometimes it is necessary to develop an application able to work with data and/or files in offline mode. Then, we could develop an application with an internal database (eg SQLite or Realm) and/or an application that can send and receive files when a network connection exists.
Dropbox is among the best cloud solution we can think of. Founded about 12 years ago, it’s a stable and efficient cloud platform.
But Let’s Go to the Core
Let’s start by saying that to integrate our application written in Swift, we will consider the official SwiftyDropbox framework and CocoaPods the dependency manager of the project.
Then, you need to register your application in the Dropbox App Console, which will create an API key that will be associated with the calls to make our project.
Now through the command line, you have to go to the directory that contains your project and create a new file called Podfile. You can do this with pod init and then add pod SwiftyDropbox.
- cd <PATH_TO_YOUR_PROJECT>
- pod init
- open-a Xcode Podfile
Your Podfile should look something like this:
Save all and close the Podfile. Now, you can tie SwiftyDropbox to the project by typing “pod install” on the terminal.
4. pod install
Congratulations! SwiftyDropbox has been correctly installed in your project with these simple steps.
Now open the project by clicking on DropBox2019.xcworkspace, and start working on the application.
Go to AppDelegate.swift and import the framework by typing “import SwiftyDropbox” to use its classes, then call the DropboxClientsManager class and assign it to the API key (which you generated at the beginning on the Dropbox App Console) in the func application.
This way, every time you start the application, the API key will be recognized.
In this example, as we said, we will see the Upload and Download functions.
So, let’s go on.
Open Main.storyboard and create three buttons, like in the following screen.
These buttons must be included in ViewController.swift because you’ll use them to invoke the Dropbox API functions.
Go to ViewController.swift and also “import SwiftyDropbox” here.
To show the related send and receive functions in action, you will create a text file with its content in the Documents folder via the native FileManager class. Then, you’ll print an output to get the file path (So you’ll know if the file has been generated in the right folder).
Documents is the folder dedicated to the file archive; every application has one. It’s generated automatically when a new iOS project is started.
Now you’ll take care of the API function, calling them in the corresponding buttons as in the following screen.
On the code, I wrote comments to make it clearer.
It’s time to run the application on your real device, but first, you need to make sure you have the Dropbox application activated.
Open and check the Dropbox app (in my case, it’s empty).
When the application will be ready and active on your device, you can see the file path generated.
If you click on the last button at “Read file into Documents”, you can read the contents of the text file on the output.
This way, you’ll be sure that the file and its contents are generated correctly.
Then, try the other two buttons, starting from “Upload to Dropbox”.
Perfect! There are no errors — but check the Dropbox app.
You should see the file generated by your application in the main Dropbox directory, and check if you open it to read the contents.
OK, now you have the last button — “Download from Dropbox”.
To get this last function (Download), I recommend you to change the contents of the text file from the Dropbox application before you click on the button.
By changing the text, you can know if the download function will be executed, because if everything goes well in the last reading control, you will see the contents of the file changed.
Now, come back to your application, click on the download button, and check if everything goes well on the output.
Perfect! There are no errors, but check the file contents again by clicking on the last button at the bottom, “Read file in Documents”.
Congratulation! Now your application works properly with your Dropbox account.
This is a simple example to work with Dropbox’s API. I decided to write it because there is no clear information on the internet about that.
I hope this helps you if you’re thinking to build an application that works on Apple devices without a network connection, where you need to save a backup of your data, or simply if you need to work with the file exchange.
You can know more about this topic by looking at the example project, available on GitHub: https://github.com/OryDS/Swift_and_Dropbox.