diff --git a/FMC Planner 2/DropboxV2ObjC.h b/FMC Planner 2/DropboxV2ObjC.h index e800829..e978df2 100644 --- a/FMC Planner 2/DropboxV2ObjC.h +++ b/FMC Planner 2/DropboxV2ObjC.h @@ -38,6 +38,8 @@ - (void)contentsOfPath:(NSString *)path completion:(void (^)(NSArray *data))handler presenter:(UIViewController *)presenter; +- (void)createFolderAtPath:(NSString *)path + presenter:(UIViewController *)presenter; #pragma mark - Setup methods diff --git a/FMC Planner 2/DropboxV2ObjC.m b/FMC Planner 2/DropboxV2ObjC.m index b334a4a..515cc0b 100644 --- a/FMC Planner 2/DropboxV2ObjC.m +++ b/FMC Planner 2/DropboxV2ObjC.m @@ -184,10 +184,8 @@ } options:0 error:nil]; - NSString *temp = - [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; - temp = [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; - request.HTTPBody = [temp dataUsingEncoding:NSASCIIStringEncoding]; + + request.HTTPBody = [self createJSONExcapedHTTPBody:data]; [NSURLConnection sendAsynchronousRequest:request @@ -300,12 +298,12 @@ } options:0 error:nil]; - NSString *temp = [[NSString alloc] initWithData:data2 - encoding:NSASCIIStringEncoding]; - temp = - [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; - [request setValue:temp.precomposedStringWithCanonicalMapping + [request setValue:[[NSString alloc] + initWithData:[self + createJSONExcapedHTTPBody:data2] + encoding:NSASCIIStringEncoding] + .precomposedStringWithCanonicalMapping forHTTPHeaderField:(@"Dropbox-API-Arg") .precomposedStringWithCanonicalMapping]; @@ -409,12 +407,12 @@ } options:0 error:nil]; - NSString *temp = [[NSString alloc] initWithData:data2 - encoding:NSASCIIStringEncoding]; - temp = - [temp stringByReplacingOccurrencesOfString:@"\\/" withString:@"/"]; - [request setValue:temp.precomposedStringWithCanonicalMapping + [request setValue:[[NSString alloc] + initWithData:[self + createJSONExcapedHTTPBody:data2] + encoding:NSASCIIStringEncoding] + .precomposedStringWithCanonicalMapping forHTTPHeaderField:(@"Dropbox-API-Arg") .precomposedStringWithCanonicalMapping]; @@ -483,6 +481,83 @@ handler(); } + +- (void)createFolderAtPath:(NSString *)path + presenter:(UIViewController *)presenter +{ + if (path == nil) { + path = _rootDirectory; + } + else { + path = [NSString stringWithFormat:@"/%@", path]; + } + NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; + request.URL = [NSURL + URLWithString:@"https://api.dropboxapi.com/2/files/create_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]; + + request.HTTPBody = [self createJSONExcapedHTTPBody:data]; + + [NSURLConnection + sendAsynchronousRequest:request + queue:[[NSOperationQueue alloc] init] + completionHandler:^(NSURLResponse *response, NSData *data, + NSError *error) { + if (error == nil) { + if ([[NSJSONSerialization JSONObjectWithData:data + options:0 + error:nil] + valueForKey:@"error_summary"] == nil) { + } + else { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [SharedDeclerations + presentErrorAlert: + [NSError + errorWithDomain:DropboxErrorDomain + code:100 + userInfo:@{ + [[NSJSONSerialization + JSONObjectWithData:data + options:0 + error:nil] + valueForKey: + DropboxErrorUserInfo] : + NSLocalizedDescriptionKey + }] + presenter:presenter + .navigationController + .topViewController]; + }]; + } + } + else if (error != nil) { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + + [SharedDeclerations + presentErrorAlert:error + presenter:presenter.navigationController + .topViewController]; + + }]; + } + + }]; +} + #pragma mark - Setup methods - (void)setupDropboxWithHandler:(void (^)(void))handler @@ -586,82 +661,12 @@ } } -- (void)createFolderAtPath:(NSString *)path - presenter:(UIViewController *)presenter +- (NSData *)createJSONExcapedHTTPBody:(NSData *)data { - if (path == nil) { - path = _rootDirectory; - } - else { - path = [NSString stringWithFormat:@"/%@", path]; - } - NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; - request.URL = [NSURL - URLWithString:@"https://api.dropboxapi.com/2/files/create_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]; - - [NSURLConnection - sendAsynchronousRequest:request - queue:[[NSOperationQueue alloc] init] - completionHandler:^(NSURLResponse *response, NSData *data, - NSError *error) { - if (error == nil) { - if ([[NSJSONSerialization JSONObjectWithData:data - options:0 - error:nil] - valueForKey:@"error_summary"] == nil) { - } - else { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - [SharedDeclerations - presentErrorAlert: - [NSError - errorWithDomain:DropboxErrorDomain - code:100 - userInfo:@{ - [[NSJSONSerialization - JSONObjectWithData:data - options:0 - error:nil] - valueForKey: - DropboxErrorUserInfo] : - NSLocalizedDescriptionKey - }] - presenter:presenter - .navigationController - .topViewController]; - }]; - } - } - else if (error != nil) { - [[NSOperationQueue mainQueue] addOperationWithBlock:^{ - - [SharedDeclerations - presentErrorAlert:error - presenter:presenter.navigationController - .topViewController]; - - }]; - } - - }]; + return [temp dataUsingEncoding:NSASCIIStringEncoding]; } @end