LoginSignup
18
18

More than 5 years have passed since last update.

[iOS DropBox連携]各種ファイル操作

Last updated at Posted at 2014-03-10

DropBox認証など(http://qiita.com/takahi5/items/40a877131b4c05b89489
が終わったあと、実際にDropBox内のファイルを操作する方法。

DBRestClientを作る

DropBoxと連携するテキスト編集アプリをつくってるとします。まずDBRestClientを準備します。DBRestClientは様々なファイル操作の入口になります。

YourViewController.m
#import <DropboxSDK/DropboxSDK.h>

@interface YourViewController () <DBRestClientDelegate>
@property (nonatomic, strong) DBRestClient *restClient;
@end
...

- (void)viewDidLoad {
    [super viewDidLoad];

    self.restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
    self.restClient.delegate = self;
}

ファイルのアップロード

DBRestClientの準備ができたら、まずはファイルのアップロードを試してみます。

[DBRestClient uploadFile:toPath:withParentRev:fromPath:]
でアプリ内のローカルファイルをDropBoxにアップロードできます。

以下はworking-draft.txtというファイルを作ってDropBoxにアップロードするサンプルコードです。
新規ファイルの場合は上書き保存を防ぐために、prentRevをnilにしておきます。
既存ファイルを上書きする場合には、parentRevに現在のrevをしていします。これはmetadataのrev属性から取得できます。

YourViewController.m
// Write a file to the local documents directory
NSString *text = @"Hello world.";
NSString *filename = @"working-draft.txt";
NSString *localDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *localPath = [localDir stringByAppendingPathComponent:filename];
[text writeToFile:localPath atomically:YES encoding:NSUTF8StringEncoding error:nil];

// Upload file to Dropbox
NSString *destDir = @"/";
[self.restClient uploadFile:filename toPath:destDir withParentRev:nil fromPath:localPath];

全てのDBRestClientのメソッドは即座に戻り値を返さず、代わりに結果を返すDBRestClientDelegateを2つ(成功/失敗)持っています。
uploadFileのcallbackは以下の2つになります。

YourViewController.m
- (void)restClient:(DBRestClient *)client uploadedFile:(NSString *)destPath
    from:(NSString *)srcPath metadata:(DBMetadata *)metadata {
    NSLog(@"File uploaded successfully to path: %@", metadata.path);
}

- (void)restClient:(DBRestClient *)client uploadFileFailedWithError:(NSError *)error {
    NSLog(@"File upload failed with error: %@", error);
}

上記のUploadFileを実行して、成功すればコンソールログに以下のようなログが出るはずです。

File uploaded successfully to path: /working-draft.txt

DropBox上のファイル一覧取得

次にDropBox上のファイル一覧を取得します。
loadMetadtaを実行します。

YourViewController.m
[self.restClient loadMetadata:@"/"];

結果は以下のコールバックで取得できます。

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

- (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error {
    NSLog(@"Error loading metadata: %@", error);
}

メタデータには現在のrevisionが格納されています。revisionはあらゆるファイル変更がなされたときに更新できます。revisionをローカルファイルにも保存しておけば、revisionを比較して更新すべきかどうか判断できます。

DropBoxからファイルをダウンロード

次にDropBox上のファイルをダウンロードしてみます。
ダウンロードには[DBRestClient loadFile:intoPath:]を使います。

適当なViewControllerで以下を実行します。
dropboxPathはDropBox内のファイルパスです。上記のDBMetadataのpath属性で取得できます。
localPathは端末内の保存したい場所のファイル名フルパスです。

YourViewController.m
[self.restClient loadFile:dropboxPath intoPath:localPath];

ダウンロード完了時に呼ばれるコールバックは以下になります。

YourViewController.m
- (void)restClient:(DBRestClient *)client loadedFile:(NSString *)localPath
    contentType:(NSString *)contentType metadata:(DBMetadata *)metadata {
    NSLog(@"File loaded into path: %@", localPath);
}

- (void)restClient:(DBRestClient *)client loadFileFailedWithError:(NSError *)error {
    NSLog(@"There was an error loading the file: %@", error);
}

DropBoxのファイルを削除

ファイル削除には[DBRestClient deletePath:dropboxPath:]を使います。

YourViewController.m
[self.restClient deletePath:dropboxPath];
18
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
18