Top Banner
Dropbox Sync API Michael Pan 13313星期三
25

Dropbox sync

Jun 02, 2015

Download

Documents

Michael Pan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Dropbox sync

Dropbox Sync APIMichael Pan

13年3月13⽇日星期三

Page 2: Dropbox sync

Who am IE-mail : [email protected]

Facebook Page : Developer’s Note

http://www.facebook.com/pages/Taipei-Taiwan/Developers-note/226724001803

Blogger : Developer’s Note

http://iosdevelopersnote.blogspot.com/

13年3月13⽇日星期三

Page 3: Dropbox sync

Official Dropbox Sync API• https://www.dropbox.com/developers/sync

13年3月13⽇日星期三

Page 4: Dropbox sync

Beware• Core API & Sync API can not work together

13年3月13⽇日星期三

Page 5: Dropbox sync

Create a Dropbox App

13年3月13⽇日星期三

Page 6: Dropbox sync

Keys

13年3月13⽇日星期三

Page 7: Dropbox sync

Keys

13年3月13⽇日星期三

Page 8: Dropbox sync

Tutorial• https://www.dropbox.com/developers/sync/tutorial/ios

13年3月13⽇日星期三

Page 9: Dropbox sync

What will we build• http://www.youtube.com/watch?v=2_-L7xg0Nac

13年3月13⽇日星期三

Page 10: Dropbox sync

Sync file• http://www.youtube.com/watch?v=8mHEq_uR1EY

13年3月13⽇日星期三

Page 11: Dropbox sync

download Framework• http://bit.ly/15K6RiR

13年3月13⽇日星期三

Page 12: Dropbox sync

#import <Dropbox/Dropbox.h>

13年3月13⽇日星期三

Page 13: Dropbox sync

URL Scheme

13年3月13⽇日星期三

Page 14: Dropbox sync

Login

13年3月13⽇日星期三

Page 15: Dropbox sync

Codes - AppDelegate.m- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ DBAccountManager* accountMgr = [[DBAccountManager alloc] initWithAppKey:@"nxsss-----" secret:@"s;a;lijejjz"]; [DBAccountManager setSharedManager:accountMgr]; DBAccount *account = accountMgr.linkedAccount; if (account) { DBFilesystem *filesystem = [[DBFilesystem alloc] initWithAccount:account]; [DBFilesystem setSharedFilesystem:filesystem]; } return YES;}- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(NSString *)source annotation:(id)annotation { DBAccount *account = [[DBAccountManager sharedManager] handleOpenURL:url]; if (account) { DBFilesystem *filesystem = [[DBFilesystem alloc] initWithAccount:account]; [DBFilesystem setSharedFilesystem:filesystem]; NSLog(@"App linked successfully!"); return YES; } return NO;}

13年3月13⽇日星期三

Page 16: Dropbox sync

View Controller- (IBAction)launchRemoteFile:(id)sender {

[[DBAccountManager sharedManager] linkFromController:self.navigationController];}

13年3月13⽇日星期三

Page 17: Dropbox sync

RemoteFileViewController

13年3月13⽇日星期三

Page 18: Dropbox sync

Storyboard

ViewController

RemoteFileViewController

13年3月13⽇日星期三

Page 19: Dropbox sync

List file system files - Button NSArray *contents = [[DBFilesystem sharedFilesystem] listFolder:[DBPath root] error:nil]; NSMutableArray * paths = [@[] mutableCopy]; for (DBFileInfo *info in contents) { if ([info isFolder]) { NSLog(@"%@/", info.path); }else{ NSLog(@"%@", info.path); } [paths addObject:info]; }

RemoteFileViewController * remoteFile = [self.storyboard instantiateViewControllerWithIdentifier:@"RemoteFile"]; remoteFile.remoteFiles = paths ; remoteFile.folderPath = [DBPath root]; [self.navigationController pushViewController:remoteFile animated:YES];

13年3月13⽇日星期三

Page 20: Dropbox sync

List file system files - must in threaddispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSArray *contents = [[DBFilesystem sharedFilesystem] listFolder:[DBPath root] error:nil]; NSMutableArray * paths = [@[] mutableCopy]; for (DBFileInfo *info in contents) { if ([info isFolder]) { NSLog(@"%@/", info.path); }else{ NSLog(@"%@", info.path); } [paths addObject:info]; } dispatch_async(dispatch_get_main_queue(), ^{ RemoteFileViewController * remoteFile = [self.storyboard instantiateViewControllerWithIdentifier:@"RemoteFile"]; remoteFile.remoteFiles = paths ; remoteFile.folderPath = [DBPath root]; [self.navigationController pushViewController:remoteFile animated:YES]; }); });

13年3月13⽇日星期三

Page 21: Dropbox sync

RemoteFileViewController.h

#import <UIKit/UIKit.h>#import <Dropbox/Dropbox.h>@interface RemoteFileViewController : UITableViewController@property (strong) NSArray * remoteFiles; // sub files@property (strong) DBPath * folderPath; // Current Folder@end

13年3月13⽇日星期三

Page 22: Dropbox sync

RemoteFileViewController.m - Observer- (void)viewDidLoad{ [super viewDidLoad]; [[DBFilesystem sharedFilesystem] addObserver:self forPathAndChildren:self.folderPath block:^{ [self reloadFolder]; }]; if (self.folderPath.name == nil || [self.folderPath.name isEqualToString:@""]) { self.navigationItem.title = @"Root"; }else{ self.navigationItem.title = self.folderPath.name; }}

-(void) dealloc{ [[DBFilesystem sharedFilesystem] removeObserver:self];}

13年3月13⽇日星期三

Page 23: Dropbox sync

ReloadFolder-(void) reloadFolder{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ NSMutableArray * files = [@[] mutableCopy]; NSArray *contents = [[DBFilesystem sharedFilesystem] listFolder:self.folderPath error:nil]; for (DBFileInfo *info in contents) { if ([info isFolder]) { NSLog(@"%@/", info.path); }else{ NSLog(@"%@", info.path); } [files addObject:info]; } dispatch_async(dispatch_get_main_queue(), ^{ self.remoteFiles = files; [self.tableView reloadData]; }); });}

13年3月13⽇日星期三

Page 24: Dropbox sync

TableViewCell- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [self.remoteFiles count];}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; DBFileInfo * fileInfo = self.remoteFiles[indexPath.row]; cell.textLabel.text = fileInfo.path.name ; if (fileInfo.isFolder) { cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; }else{ cell.accessoryType = UITableViewCellAccessoryNone; } return cell;}

13年3月13⽇日星期三

Page 25: Dropbox sync

Demo

13年3月13⽇日星期三