diff --git a/plugin.xml b/plugin.xml index 8a55b5d..d8b02ed 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,31 +1,39 @@ - - - File Opener2 - A File Opener Plugin for Cordova. - MIT - - - - - - - - - - - - - - - - - - - - - - - + + + + File Opener2 + A File Opener Plugin for Cordova. + MIT + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ios/FileOpener2.h b/src/ios/FileOpener2.h new file mode 100644 index 0000000..8a684ca --- /dev/null +++ b/src/ios/FileOpener2.h @@ -0,0 +1,11 @@ +#import + +@interface FileOpener2 : CDVPlugin { + NSString *localFile; +} + +@property(nonatomic, strong) UIDocumentInteractionController *controller; + +- (void) open: (CDVInvokedUrlCommand*)command; + +@end \ No newline at end of file diff --git a/src/ios/FileOpener2.m b/src/ios/FileOpener2.m new file mode 100644 index 0000000..9df9a4b --- /dev/null +++ b/src/ios/FileOpener2.m @@ -0,0 +1,59 @@ +#import "FileOpener2.h" +#import + +#import +#import + +@implementation FileOpener2 +@synthesize controller = docController; + +- (void) open: (CDVInvokedUrlCommand*)command { + + NSString *path = [command.arguments objectAtIndex:0]; + //NSString *uti = [command.arguments objectAtIndex:1]; + + CDVViewController* cont = (CDVViewController*)[ super viewController ]; + + NSArray *dotParts = [path componentsSeparatedByString:@"."]; + NSString *fileExt = [dotParts lastObject]; + + NSString *uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL); + + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + //NSLog(@"path %@, uti:%@", path, uti); + NSURL *fileURL = nil; + + fileURL = [NSURL URLWithString:path]; + localFile = fileURL.path; + + dispatch_async(dispatch_get_main_queue(), ^{ + + docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL]; + docController.delegate = self; + docController.UTI = uti; + + CGRect rect = CGRectMake(0, 0, 1000.0f, 150.0f); + CDVPluginResult* pluginResult = nil; + BOOL wasOpened = [docController presentOptionsMenuFromRect:rect inView:cont.view animated:NO]; + //presentOptionsMenuFromRect + //presentOpenInMenuFromRect + + if(wasOpened) { + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""]; + //NSLog(@"Success"); + } else { + NSDictionary *jsonObj = [ [NSDictionary alloc] + initWithObjectsAndKeys : + @"9", @"status", + @"Could not handle UTI", @"message", + nil + ]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj]; + //NSLog(@"Could not handle UTI"); + } + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; + }); + }); +} + +@end