Supporting multiple Dropbox accounts

Last week we launched the all-new Dropbox for Business which allows users to have both a work and a personal Dropbox. When building an app using the Dropbox API, it’s important to consider that users may have more than one Dropbox and to design your app accordingly.

When a user connects with Dropbox via your app, they will be prompted to choose which Dropbox they would like to use for your app. If you allow the user to connect more than one time, they will be able to connect multiple Dropbox accounts with your app.

You can see an example of supporting multiple Dropbox accounts in our Notes example app, available in the Dropbox Sync SDK for iOS. The app has a button to “Link” your Dropbox account, then displays all linked accounts. You can see here that I’ve connected both my work and personal accounts. I also have the option to “Unlink” either account.

When I select an account, I can view the notes I’ve saved for that account. The iOS SDK for the Sync API allows you to initialize a DBFilesystem with a particular account, like so:

DBAccount *account = [self accountForSection:[indexPath section]];
DBFilesystem *filesystem = [[DBFilesystem alloc] initWithAccount:account];

NotesFolderListController *controller = [[NotesFolderListController alloc] initWithFilesystem:filesystem root:[DBPath root]];
[self.navigationController pushViewController:controller animated:YES];

The Dropbox Core API and Datastore API also work well with multiple accounts.

Here’s what it looks like to initialize a DBRestClient in the Core API with a specific account:

// In this example we pick the first account, but you'll want to let the user choose.
NSArray *userId = [DBSession sharedSession].userIds[0];
DBRestClient *restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession] userId:userId];

Here’s an example of opening a datastore for a specific account:

// In this example we pick the first account, but you'll want to let the user choose.
DBAccount *account = [DBAccountManager sharedManager].linkedAccounts[0];
DBDatastore *datastore = [DBDatastore openDefaultStoreForAccount:account error:nil];

These are just a few quick examples of working with multiple accounts in the Dropbox iOS SDKs. If you need any additional help with supporting multiple accounts please feel free to contact us via the developer forum.