Dropboxへアクセスするiphoneアプリを作成する手順

dropboxのWeb上でアプリケーションを登録

以下にアクセスしてアプリ登録を行う

https://www.dropbox.com/developers/apps


「create an app」ボタンをクリックし、「core API」を選択し、
「App name」に任意の名前を入力して「Full Dropbox」を選択。

※Platformは必要な項目を選択すること

登録後に以下の画面で「App key」と「App secret」を確認。

f:id:yuriken27:20130609232643p:plain:w400

■DropboxSDKをダウンロード

以下のページからiOS用のSDKをダウンロードする。

https://www.dropbox.com/developers/core/sdk

xcodeでアプリケーションを作成

xcodeプロジェクト作成とDropboxFrameworkを追加

Single View Applicationで新規プロジェクトを作成。

アプリの対象バージョンを「5.0」に変更。

作成したxcodeプロジェクトにDropboxFrameworkを追加。

FinderでSDKに入っている「DropboxSDK.framework」ディレクトリを
xcodeに移動する。

f:id:yuriken27:20130609235106p:plain

移動後、以下の画面のように設定してOKを押す。
f:id:yuriken27:20130609235457p:plain

このほか、以下の2つを追加
・QuartzCore.framework
・Security.framework

f:id:yuriken27:20130610001037p:plain

AppDelegate.m

インポート文を追加

#import <DropboxSDK/DropboxSDK.h>

「application:didFinishLaunchingWithOptions」に以下のコードを追加

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    
    // ここから追加
    DBSession* dbSession =
    [[DBSession alloc]
     initWithAppKey:@"xxxxxxxxxxxxxxx"
     appSecret:@"xxxxxxxxxxxxxxx"
     root:kDBRootDropbox]; // either kDBRootAppFolder or kDBRootDropbox
    [DBSession setSharedSession:dbSession];
    // ここまで

    return YES;
}
// 以下の関数も追加
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([[DBSession sharedSession] handleOpenURL:url]) {
        if ([[DBSession sharedSession] isLinked]) {
            NSLog(@"App linked successfully!");
            // At this point you can start making API calls
        }
        return YES;
    }
    // Add whatever other url handling code your app requires here
    return NO;
}

「xxxxxxxxxxxxxxx」には、DropboxのWebで登録したアプリの「App key」と「App secret」を登録

ViewController.h

DropboxSDK.hのインポート追加と「DBRestClientDelegate」デリゲートを追加し、
DBRestClientのプロパティをセット。

#import <UIKit/UIKit.h>
#import <DropboxSDK/DropboxSDK.h>

@interface ViewController : UIViewController <DBRestClientDelegate>

@property (nonatomic, readonly) DBRestClient *restClient;

@end

ViewController.m

まずは、先頭付近にrestClientのsynthesizeを追加。

@implementation ViewController
@synthesize restClient = _restClient;


以下のコードを追加。

- (DBRestClient *)restClient {
    if (!_restClient) {
        _restClient =
        [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
        _restClient.delegate = self;
    }
    return _restClient;
}

// ビュー表示後に、ログイン済みかチェックし、未ログインならばログイン画面を表示
-(void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (![[DBSession sharedSession] isLinked]) {
        [[DBSession sharedSession] linkFromController:self];
    }
    // Dropbox読み込み
    [[self restClient] loadMetadata:@"/"];   
}

Dropbox読み込みのコールバック関数を追加

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if ([[DBSession sharedSession] handleOpenURL:url]) {
        if ([[DBSession sharedSession] isLinked]) {
            NSLog(@"App linked successfully!");
            // At this point you can start making API calls
        }
        return YES;
    }
    // Add whatever other url handling code your app requires here
    return NO;
}

- (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
    if (metadata.isDirectory) {
        NSLog(@"Folder '%@' contains:", metadata.path);
        for (DBMetadata *file in metadata.contents) {
            NSLog(@"\t%@", file.filename);
        }
    }
}

DropboxTest-Info.plist

ファイルを右クリックし、「Open As」ー「Source Code」で開き、
ファイルの最後のとの間に以下のコードを挿入。

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>db-APP_KEY</string>
            </array>
        </dict>
    </array>

「db-APP_KEY」のAPP_KEYにDropboxのWebで登録したアプリの「App key」をセット。

例:db-xxxxxxxxxxxxxxx

起動テスト

アプリを起動して以下の画面が表示されたれ
自分のID、パスワードを指定してOKを押す。

f:id:yuriken27:20130610005206p:plain

コンソールにDropboxのトップディレクトリのファイル、ディレクトリ内容が表示されたら
ログインに成功!