Channels ▼
RSS

Web Development

Working with Dropbox


Linking to the Account

The FooDrop class uses the DBLoginControllerDelegate protocol to handle a login session (Listing Two). This protocol supplies the callback delegates for that session.

Listing Three shows how FooDrop would implement the login session. First, it declares the prototypes for two action handlers. Action doLink: starts a login session, while doUnlink: ends it.

Listing Three

/*
--
-- FooDrop.h
--
*/
#import "DropboxSDK.h"

@class DBRestClient;
@interface FooDrop : NSObject <DBLoginControllerDelegate, 
			DBRestClientDelegate>

// -- see Listing Two

// action prototypes
- (IBAction) doLink:(id)aSrc;
- (IBAction) doUnlink:(id)aSrc;
@end

/*
--
-- FooDrop.m
--
*/
@implementation FooDrop

// -- see Listing Two

// -- ACTION HANDLERS
// Start a Dropbox session
- (IBAction) doLink:(id)aSrc
{
	DBSession *tDbx;
	
	tDbx = [DBSession sharedSession];
	if (tDbx != nil)
	{
		if (![tDbx isLinked])
		{
			DBLoginController* tDbx 
				= [[DBLoginController new] autorelease];
			tDbx.delegate = self;
			[tDbx presentFromController:self];
		}
	}
}


// End a Dropbox session
- (IBAction) doUnlink:(id)aSrc
{
	DBSession *tDbx;
	
	tDbx = [DBSession sharedSession];
	if (tDbx != nil)
	{
		if ([tDbx isLinked])
			[tDbx unlink];
	}
}

// -- DELEGATE METHODS
// Login was successful
- (void)loginControllerDidLogin:(DBLoginController*)aSrc 
{
	// create the REST client
	pOut_ = [[DBRestClient alloc] 
		initWithSession:[DBSession sharedSession]];
	self.dboxClient.delegate = self;
}

// Login was cancelled
- (void)loginControllerDidCancel:(DBLoginController*)aSrc 
{
	// handle the cancellation here
}

     @end

Action doLink: checks DBSession for a valid shared session. Then it checks if the session is not linked to a user account. If both checks are true, doLink: creates an instance of DBLoginController. It sets FooDrop as the delegate, and it invokes presentFromController: to display the standard login screen.

Action doUnlink: also checks DBSession for a shared session. But this time, it checks if the session has an active link. If the link exists, the action breaks it using the unlink method.


Figure 3: Typical login screen.

Figure 3 shows a typical Dropbox login screen. Users enter their account details on the fields provided. When they click the Link button, FooDrop invokes the delegate method loginControllerDidLogin: (Listing Three, lines 63-69). This method creates an instance of DBRestClient and stores it into the pOut_ property. Then it sets FooDrop as the delegate for DBRestClient. When users click the Cancel button, FooDrop invokes the delegate method loginControllerDidCancel:. This one provides the clean-up code for the login attempt.

Both delegate methods have the DBLoginController instance as their sole inputs.

Client Folder

The first task for a custom client is to create its own folder on the user's Dropbox account. Moreover, the folder must use the same name as the client. So, if the client's name is FooDrop, its folder must also be named FooDrop.

Creating the folder is done with the DBRestClient object. First, define the folder name as an NSString. Then pass the NSString object to the instance method createFolder:.

tDir = [NSString stringWithString:@"FooDrop"];
[self.dboxClient createFolder:tDir];

Make sure the name does not contain any invalid characters like a forward slash (/) or colon (:). Otherwise, the method will fail.

The client will try to place the new folder at the root level of the user's account. It then invokes one of many delegate methods from the protocol DBRestClientDelegate (Figure 4).


Figure 4.

The method restClient:createdFolder: runs when the client succeeds in creating the desired folder. It gets two input arguments: the DBRestClient instance and a DBMetadata object. The latter object describes the newly made folder.

- (void)restClient:(DBRestClient*)aSrc 
	createdFolder:(DBMetadata*)aDir 
{ 
	// handle the new Dropbox folder…
}

If the client could not create the folder, it runs the method restClient:createFolderFailedWithError:. This, too, gets two input arguments: the same DBRestClient instance and an NSError object. The latter object describes the reason why the task failed.

- (void)restClient:(DBRestClient*)client 
	createFolderFailedWithError:(NSError*)aErr 
{
	// handle the Dropbox error…
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV