Skip to content

Commit

Permalink
Fixed typo in method name and now doing file download in background o…
Browse files Browse the repository at this point in the history
…n iOS
  • Loading branch information
Mario Eberhard committed Feb 5, 2014
1 parent 96efa81 commit 5726044
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/android/DocumentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

public class DocumentHandler extends CordovaPlugin {

public static final String HANDLE_DOCUMENT_ACTION = "HandleDocumentWihtURL";
public static final String HANDLE_DOCUMENT_ACTION = "HandleDocumentWithURL";
public static final int ERROR_NO_HANDLER_FOR_DATA_TYPE = 53;
public static final int ERROR_UNKNOWN_ERROR = 1;

Expand Down
47 changes: 26 additions & 21 deletions src/ios/DocumentHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,38 @@

@implementation DocumentHandler

- (void)HandleDocumentWihtURL:(CDVInvokedUrlCommand*)command;
- (void)HandleDocumentWithURL:(CDVInvokedUrlCommand*)command;
{
CDVPluginResult *commandResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:@""];

NSDictionary* dict = [command.arguments objectAtIndex:0];
__weak DocumentHandler* weakSelf = self;

NSString* urlStr = dict[@"url"];
NSLog(@"Got url: %@", urlStr);
NSURL* url = [NSURL URLWithString:urlStr];
NSData* dat = [NSData dataWithContentsOfURL:url];
NSString* fileName = [url lastPathComponent];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent: fileName];
NSURL* tmpFileUrl = [[NSURL alloc] initFileURLWithPath:path];
[dat writeToURL:tmpFileUrl atomically:YES];
self.fileUrl = tmpFileUrl;

dispatch_async(dispatch_get_main_queue(), ^{
QLPreviewController* cntr = [[QLPreviewController alloc] init];
cntr.delegate = self;
cntr.dataSource = self;
dispatch_queue_t asyncQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(asyncQueue, ^{

NSDictionary* dict = [command.arguments objectAtIndex:0];

NSString* urlStr = dict[@"url"];
NSURL* url = [NSURL URLWithString:urlStr];
NSData* dat = [NSData dataWithContentsOfURL:url];
NSString* fileName = [url lastPathComponent];
NSString* path = [NSTemporaryDirectory() stringByAppendingPathComponent: fileName];
NSURL* tmpFileUrl = [[NSURL alloc] initFileURLWithPath:path];
[dat writeToURL:tmpFileUrl atomically:YES];
weakSelf.fileUrl = tmpFileUrl;

dispatch_async(dispatch_get_main_queue(), ^{
QLPreviewController* cntr = [[QLPreviewController alloc] init];
cntr.delegate = weakSelf;
cntr.dataSource = weakSelf;

UIViewController* root = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[root presentViewController:cntr animated:YES completion:nil];
});

UIViewController* root = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[root presentViewController:cntr animated:YES completion:nil];

[weakSelf.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
});


[self.commandDelegate sendPluginResult:commandResult callbackId:command.callbackId];
}

#pragma mark - QLPreviewController data source
Expand Down
2 changes: 1 addition & 1 deletion www/DocumentHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var myFunc = function (
successHandler,
failureHandler,
"DocumentHandler",
"HandleDocumentWihtURL",
"HandleDocumentWithURL",
[{"url" : url}]);
};

Expand Down

0 comments on commit 5726044

Please sign in to comment.