Upload a text file to Dropbox on iOS

Here we'll show you how to use our iOS SDK to upload a file to Dropbox.

Get Set Up

1 Log in to Temboo. If you don't already have an account, you can register for free.

2Make sure you've downloaded the Temboo iOS SDK and that you've added it to your development environment as described in our iOS getting started tutorial.

3You'll also need a valid Dropbox account and a Dropbox app, which you can create in the Dropbox App Console. The app name and domain can be whatever you like. Make sure to use the settings shown in the screenshot below when creating your new Dropbox app:

The Dropbox app console with the app settings you need for this example

The Dropbox app console with the app settings you need for this example

4With your Dropbox application in place, visit the Dropbox OAuth bundle. Starting with the InitializeOAuth Choreo, use these Choreos to grant your Dropbox application access to interact with Dropbox on your behalf. This short video demonstrates how to use our OAuth Choreos, and it's a good idea to take a break and watch it now before you dive into this step. You'll need the access tokens returned by this process to complete the rest of this tutorial.

5 Go to the Dropbox > FilesAndMetadata > UploadFile Choreo in our Library. Select iOS from the drop down menu at the top of the page. Enter your Dropbox access tokens and click on the Save Profile button to save your Dropbox app details. Make sure to fill in "sandbox" for Root input. Now you can reuse these Dropbox authentication details on any Dropbox Choreo in our Library. Using a Profile will also make your code simpler and more secure.

Test the Choreo

6 Let's test the Dropbox > FilesAndMetadata > UploadFile Choreo directly from our website. Start by inserting your Dropbox account info that you saved earlier. Next, enter a FileName (like "test.txt") and finally put something into the FileContents input (like "Hello World!").

7 Click Generate Code and you'll see some details about your uploaded file in the Output section. If you go to your app folder in Dropbox, you'll see your file appear!

Base64 Encoding (or "Help! I can't read the text")

8 If you open your uploaded file in Dropbox, you'll see some garbled text. That's because the Dropbox UploadFile Choreo needs a Base64 encoded string for its FileContents input. Luckily enough we have one of these Choreos in our Temboo Library. Go to the Utilities > Encoding > Base64Encode Choreo to get started.

9 Type "Hello world" into the Text input and click Generate Code. You should see a Base64 translation appear in the output. If you pass this text to the Dropbox Choreo it will create the file in Dropbox with human-readable text as expected.

Combining Choreos

10 To finish this example up, let's combine the two Choreos in one iOS class. A few things to note in the code block below:

To use the code below, simply substitute in your Dropbox Profile's name and run the code. Your new upload should appear in your Dropbox folder with the text you've specified as its contents.

#import <Foundation/Foundation.h>
#import "TMBChoreography.h"
#import "TMBTembooSession.h"
#import "TMBDropbox.h"
#import "TMBUtilities.h"

@interface UploadFile : NSObject <TMBChoreographyDelegate>
@property (readwrite, nonatomic, strong) TMBTembooSession* session;
-(id)initWithSession:(TMBTembooSession *)s;

@implementation UploadFile

// Initialize property
@synthesize session;

// Class initializer accepting Temboo Session as an argument
-(id)initWithSession:(TMBTembooSession *)s {
	if(self = [super init]){
		// Set our local property
		self.session = s;
	return self;

-(void)runUploadFileChoreo {
    // Instantiate the Choreo, using a previously instantiated TembooSession object, eg:
    TMBUtilities_Encoding_Base64Encode *base64EncodeChoreo = [[TMBUtilities_Encoding_Base64Encode alloc] initWithSession:session];
    // Get an InputSet object for the choreo
    TMBUtilities_Encoding_Base64Encode_Inputs *base64EncodeInputs = [base64EncodeChoreo newInputSet];
    // Set inputs
    [base64EncodeInputs setText:@"You just created this file with Temboo's iOS SDK!"];
    // Execute choreo -- implement methods below to be notified of result/error
    [base64EncodeChoreo executeWithInputs:base64EncodeInputs delegate:self];

-(void)uploadToFile:(NSString*)base64EncodedText {
    // Instantiate the Choreo, using a previously instantiated TembooSession object, eg:
    TMBDropbox_FilesAndMetadata_UploadFile *uploadFileChoreo = [[TMBDropbox_FilesAndMetadata_UploadFile alloc] initWithSession:session];

    // Get an InputSet object for the choreo
    TMBDropbox_FilesAndMetadata_UploadFile_Inputs *uploadFileInputs = [uploadFileChoreo newInputSet];

    // Set Profile to use for execution
    [uploadFileInputs setCredential:@"YOUR_DROPBOX_TEMBOO_PROFILE"];

    // Create a date stamp to append to our file name
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd-HH:mm:ss"];
    NSDate *date = [NSDate date];
    NSString *dateStamp = [dateFormatter stringFromDate:date];
    // Set inputs
    [uploadFileInputs setFileName:[NSString stringWithFormat:@"temboo-iOS-tutorial-%@.txt", dateStamp]];
    [uploadFileInputs setFileContents:base64EncodedText];

    // Execute choreo -- implement methods below to be notified of result/error
    [uploadFileChoreo executeWithInputs:uploadFileInputs delegate:self];

-(void)choreographyDidFailWithError:(NSError*)error {
    NSLog(@"%s - An error occured: %@", __PRETTY_FUNCTION__, error);

-(void)choreographyDidFinishExecuting:(id)result {
    if([result isKindOfClass:[TMBUtilities_Encoding_Base64Encode_ResultSet class]]){
        // This is the result of the Base64 choreo, pass the results to the file upload method
        NSString *encoded = [result getBase64EncodedText];
        NSLog(@"%s - Got Base64 encoded text: %@", __PRETTY_FUNCTION__, encoded);
        [self uploadToFile:encoded];
        NSString *response = [result getResponse];
        NSLog(@"%s - Dropbox upload response: %@", __PRETTY_FUNCTION__, response);


int main(int argc, const char * argv[]) {
    @autoreleasepool {
    	// Create our session once, so that we can reuse it across choreography calls
        TMBTembooSession *session = [[TMBTembooSession alloc] initWithAccount:@"ACCOUNT_NAME" appKeyName:@"APP_NAME" andAppKeyValue:@"APP_KEY"];
        UploadFile *tutorial = [[UploadFile alloc] initWithSession:session];
        [tutorial runUploadFileChoreo];
        [[NSRunLoop currentRunLoop] run];
    return 0;

What next?

We're all finished! This iOS application can create a text file out of any string you might produce while coding. Check out the rest of the 2000+ Choreos in our Library and discover all sorts of valuable data that you could save to Dropbox.

Once you've got your code up and running, you're ready to move on and do more. From monitoring your running applications, to moving your generated Temboo code to your preferred development environment and sharing it with colleagues, collaborators and friends - we've got you covered.

Get Live Updates About Your Application

Integrate Temboo with Slack to get up-to-the-second notifications and alerts about your deployed Temboo code.

Commit Code to GitHub

Commit Temboo-generated code directly to GitHub and share with the world. Learn more.

Need help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.