Upload
michael-pan
View
1.425
Download
0
Embed Size (px)
Citation preview
Dropbox Sync APIMichael Pan
13年3月13⽇日星期三
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⽇日星期三
Official Dropbox Sync API• https://www.dropbox.com/developers/sync
13年3月13⽇日星期三
Beware• Core API & Sync API can not work together
13年3月13⽇日星期三
Create a Dropbox App
13年3月13⽇日星期三
Keys
13年3月13⽇日星期三
Keys
13年3月13⽇日星期三
Tutorial• https://www.dropbox.com/developers/sync/tutorial/ios
13年3月13⽇日星期三
What will we build• http://www.youtube.com/watch?v=2_-L7xg0Nac
13年3月13⽇日星期三
Sync file• http://www.youtube.com/watch?v=8mHEq_uR1EY
13年3月13⽇日星期三
#import <Dropbox/Dropbox.h>
13年3月13⽇日星期三
URL Scheme
13年3月13⽇日星期三
Login
13年3月13⽇日星期三
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⽇日星期三
View Controller- (IBAction)launchRemoteFile:(id)sender {
[[DBAccountManager sharedManager] linkFromController:self.navigationController];}
13年3月13⽇日星期三
RemoteFileViewController
13年3月13⽇日星期三
Storyboard
ViewController
RemoteFileViewController
13年3月13⽇日星期三
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⽇日星期三
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⽇日星期三
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⽇日星期三
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⽇日星期三
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⽇日星期三
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⽇日星期三
Demo
13年3月13⽇日星期三