dropbox deauth finalized, download of files (array in loop) implemented

This commit is contained in:
Kilian Hofmann 2016-03-30 22:51:27 +02:00
parent eb9b485d1c
commit f0365779b1
4 changed files with 144 additions and 25 deletions

View File

@ -13,15 +13,16 @@
@interface DropboxV2ObjC : NSObject @interface DropboxV2ObjC : NSObject
@property(strong, nonatomic) NSString *token; @property(strong, nonatomic) NSString *token;
/// No trailing slash
@property(strong, nonatomic) NSString *rootDirectory; @property(strong, nonatomic) NSString *rootDirectory;
@property(strong, nonatomic) NSData *kJSONNullObject; @property(strong, nonatomic) NSData *kJSONNullObject;
@property(strong, nonatomic) NSURLProtectionSpace *kDropboxProtectionSpace; @property(strong, nonatomic) NSURLProtectionSpace *kDropboxProtectionSpace;
- (BOOL)authorizeUserWithToke:(NSURL *)token completion:(void (^)(void))handler; - (BOOL)authorizeUserWithToke:(NSURL *)token completion:(void (^)(void))handler;
- (NSData *)downloadFromDropbox; - (void)downloadFromDropbox:(NSArray *)files;
- (void)uploadToDropbox:(NSData *)data; - (void)uploadToDropbox:(NSData *)data;
- (NSArray *)contentsOfPath:(NSString *)path; - (NSArray *)contentsOfPath:(NSString *)path;
- (NSDictionary *)getUserInfo; - (NSDictionary *)getUserInfo;
- (BOOL)deauthorizeUser; - (void)deauthorizeUser;
@end @end

View File

@ -9,10 +9,9 @@
#import "DropboxV2ObjC.h" #import "DropboxV2ObjC.h"
@implementation DropboxV2ObjC @implementation DropboxV2ObjC
- (DropboxV2ObjC *)init { - (DropboxV2ObjC *)init {
self = [super init]; self = [super init];
self.rootDirectory = @"/Apps/FMC Planner 2/"; self.rootDirectory = @"";
self.kJSONNullObject = [@"null" dataUsingEncoding:NSASCIIStringEncoding]; self.kJSONNullObject = [@"null" dataUsingEncoding:NSASCIIStringEncoding];
self.kDropboxProtectionSpace = self.kDropboxProtectionSpace =
[[NSURLProtectionSpace alloc] initWithHost:@"dropbox" [[NSURLProtectionSpace alloc] initWithHost:@"dropbox"
@ -24,7 +23,6 @@
} }
#pragma mark - OAuth stuff #pragma mark - OAuth stuff
- (BOOL)authorizeUserWithToke:(NSURL *)token - (BOOL)authorizeUserWithToke:(NSURL *)token
completion:(void (^)(void))handler { completion:(void (^)(void))handler {
NSScanner *scan = [NSScanner scannerWithString:token.absoluteString]; NSScanner *scan = [NSScanner scannerWithString:token.absoluteString];
@ -52,18 +50,131 @@
return true; return true;
} }
- (BOOL)deauthorizeUser { - (void)deauthorizeUser {
NSDictionary *credentials = NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[[NSURLCredentialStorage sharedCredentialStorage] request.URL = [NSURL
credentialsForProtectionSpace:_kDropboxProtectionSpace]; URLWithString:@"https://api.dropboxapi.com/1/disable_access_token"];
NSURLCredential *credential = [credentials.objectEnumerator nextObject]; [request addValue:[NSString stringWithFormat:@"Bearer %@", _token]
[[NSURLCredentialStorage sharedCredentialStorage] .precomposedStringWithCanonicalMapping
removeCredential:credential forHTTPHeaderField:(@"Authorization")
forProtectionSpace:_kDropboxProtectionSpace]; .precomposedStringWithCanonicalMapping];
return true; [request addValue:(@"application/json")
.precomposedStringWithCanonicalMapping
forHTTPHeaderField:(@"Content-Type")
.precomposedStringWithCanonicalMapping];
request.HTTPMethod = @"POST";
request.HTTPBody = _kJSONNullObject;
[NSURLConnection sendAsynchronousRequest:request
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data, NSError *error) {
if ([[NSJSONSerialization
JSONObjectWithData:data
options:0
error:nil] isEqualToDictionary
: @{}]) {
NSDictionary *credentials =
[[NSURLCredentialStorage sharedCredentialStorage]
credentialsForProtectionSpace:
_kDropboxProtectionSpace];
NSURLCredential *credential =
[credentials.objectEnumerator nextObject];
[[NSURLCredentialStorage sharedCredentialStorage]
removeCredential:credential
forProtectionSpace:_kDropboxProtectionSpace];
}
}];
} }
#pragma mark - File and directory operations #pragma mark - File and directory operations
/**
* Pass nil as path for root dir
* @return Content of directory at path or nil if error
*/
- (NSArray *)contentsOfPath:(NSString *)path {
if (path == nil) {
path = _rootDirectory;
} else {
path = [path
stringByAppendingString:[NSString stringWithFormat:@"/%@", path]];
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.URL =
[NSURL URLWithString:@"https://api.dropboxapi.com/2/files/list_folder"];
[request addValue:[NSString stringWithFormat:@"Bearer %@", _token]
.precomposedStringWithCanonicalMapping
forHTTPHeaderField:(@"Authorization")
.precomposedStringWithCanonicalMapping];
[request addValue:(@"application/json")
.precomposedStringWithCanonicalMapping
forHTTPHeaderField:(@"Content-Type")
.precomposedStringWithCanonicalMapping];
request.HTTPMethod = @"POST";
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
@"path" : path.precomposedStringWithCanonicalMapping
}
options:0
error:nil];
NSString *temp =
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
request.HTTPBody = [temp dataUsingEncoding:NSASCIIStringEncoding];
NSURLResponse *response = nil;
NSError *error = nil;
id dataOut =
[self parseJSON:[NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error]];
if ([dataOut isKindOfClass:[NSDictionary class]]) {
return [dataOut valueForKey:@"entries"];
} else {
return nil;
}
}
- (void)downloadFromDropbox:(NSArray *)files {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.URL = [NSURL
URLWithString:@"https://content.dropboxapi.com/2/files/download"];
[request addValue:[NSString stringWithFormat:@"Bearer %@", _token]
.precomposedStringWithCanonicalMapping
forHTTPHeaderField:(@"Authorization")
.precomposedStringWithCanonicalMapping];
[request addValue:(@"").precomposedStringWithCanonicalMapping
forHTTPHeaderField:(@"Content-Type")];
request.HTTPMethod = @"POST";
request.HTTPBody = nil;
// File Path
for (NSString *string in files) {
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
@"path" : [NSString stringWithFormat:@"/%@", string]
.precomposedStringWithCanonicalMapping
}
options:0
error:nil];
NSString *temp =
[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
temp =
[temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"];
[request addValue:temp.precomposedStringWithCanonicalMapping
forHTTPHeaderField:(@"Dropbox-API-Arg")
.precomposedStringWithCanonicalMapping];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *dataOut = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
[dataOut writeToFile:[SharedDeclerations savePathForFile:string]
atomically:YES];
}
}
#pragma mark - Other operations #pragma mark - Other operations

View File

@ -84,14 +84,15 @@
- (void)tableView:(UITableView *)tableView - (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath { didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self if ([self.title isEqualToString:@"Manage"]) {
dismissViewControllerAnimated:YES [self dismissViewControllerAnimated:YES
completion:^(void) { completion:^(void) {
[_main [_main loadSave:[tableView
loadSave:[tableView cellForRowAtIndexPath:
cellForRowAtIndexPath:indexPath] indexPath]
.textLabel.text]; .textLabel.text];
}]; }];
}
} }
@end @end

View File

@ -46,9 +46,8 @@
#pragma mark - Dropbox Button #pragma mark - Dropbox Button
// Standard action (hence set), dnamically set and unset
- (IBAction)dbConnect:(UIButton *)sender { - (IBAction)dbConnect:(UIButton *)sender {
DropboxV2ObjC *dbClient =
((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient;
UIViewController *webController = [[UIViewController alloc] init]; UIViewController *webController = [[UIViewController alloc] init];
webController.title = @"Dropbox"; webController.title = @"Dropbox";
UIWebView *web = [[UIWebView alloc] initWithFrame:webController.view.frame]; UIWebView *web = [[UIWebView alloc] initWithFrame:webController.view.frame];
@ -65,6 +64,7 @@
@"uri=x-fmc:/"]]]; @"uri=x-fmc:/"]]];
} }
// Dynamically set and unset
- (IBAction)dbDisconnect:(UIButton *)sender { - (IBAction)dbDisconnect:(UIButton *)sender {
DropboxV2ObjC *dbClient = DropboxV2ObjC *dbClient =
((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient; ((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient;
@ -83,7 +83,13 @@
- (IBAction)testing:(id)sender { - (IBAction)testing:(id)sender {
DropboxV2ObjC *dbClient = DropboxV2ObjC *dbClient =
((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient; ((AppDelegate *)[UIApplication sharedApplication].delegate).dbClient;
[dbClient getUserInfo]; NSLog(@"%@", [dbClient contentsOfPath:nil]);
[dbClient downloadFromDropbox:@[
@"lol.txt",
@"rofl.txt",
@"tldr.txt",
@"lol Kopie 3.txt"
]];
} }
@end @end