Manually merged PR #107

This commit is contained in:
pwlin 2017-04-06 14:44:37 +02:00
parent de09dc0185
commit 9a0c6dfb5c
2 changed files with 55 additions and 42 deletions

View File

@ -28,6 +28,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}
@property(nonatomic, strong) UIDocumentInteractionController *controller;
@property(nonatomic, strong) CDVViewController *cdvViewController;
- (void) open: (CDVInvokedUrlCommand*)command;

View File

@ -27,25 +27,27 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#import <MobileCoreServices/MobileCoreServices.h>
@implementation FileOpener2
@synthesize controller = docController;
- (void) open: (CDVInvokedUrlCommand*)command {
NSString *path = command.arguments[0];
NSString *uti = command.arguments[1];
if (!uti || (NSNull*)uti == [NSNull null]) {
NSArray *dotParts = [path componentsSeparatedByString:@"."];
NSString *fileExt = [dotParts lastObject];
NSString *path = [[command.arguments objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *contentType = nil;
uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
if([command.arguments count] == 2) { // Includes contentType
contentType = [command.arguments objectAtIndex:1];
}
CDVViewController* cont = (CDVViewController*)[super viewController];
self.cdvViewController = cont;
NSArray *dotParts = [path componentsSeparatedByString:@"."];
NSString *fileExt = [dotParts lastObject];
NSString *uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
dispatch_async(dispatch_get_main_queue(), ^{
// TODO: test if this is a URI or a path
// Use one of the following lines
//NSURL *fileURL = [NSURL URLWithString:path];
NSURL *fileURL = [NSURL fileURLWithPath:path];
NSURL *fileURL = [NSURL URLWithString:path];
localFile = fileURL.path;
@ -54,30 +56,40 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
if(![fm fileExistsAtPath:localFile]) {
NSDictionary *jsonObj = @{@"status" : @"9",
@"message" : @"File does not exist"};
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:jsonObj];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}
self.controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
self.controller.delegate = self;
self.controller.UTI = uti;
docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;
docController.UTI = uti;
CGRect rect = CGRectMake(0, 0, 1000.0f, 150.0f);
CDVPluginResult* pluginResult = nil;
BOOL wasOpened = [self.controller presentOptionsMenuFromRect:rect inView:cont.view animated:NO];
//Opens the file preview
BOOL wasOpened = [docController presentPreviewAnimated: NO];
if(wasOpened) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
//NSLog(@"Success");
} else {
NSDictionary *jsonObj = @{@"status" : @"9",
@"message" : @"Could not handle UTI"};
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:jsonObj];
NSDictionary *jsonObj = [ [NSDictionary alloc]
initWithObjectsAndKeys :
@"9", @"status",
@"Could not handle UTI", @"message",
nil
];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
}
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
});
}
@end
@implementation FileOpener2 (UIDocumentInteractionControllerDelegate)
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self.cdvViewController;
}
@end