mirror of
https://gitee.com/shuto/cordova-plugin-file-opener2.git
synced 2025-01-31 12:02:49 +08:00
Fix double-escaping of input URI. Prefer supplied UTI if available. Modernize Objective-C usage.
This commit is contained in:
parent
2686c86ac3
commit
755e722cc8
@ -27,57 +27,47 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||||||
#import <MobileCoreServices/MobileCoreServices.h>
|
#import <MobileCoreServices/MobileCoreServices.h>
|
||||||
|
|
||||||
@implementation FileOpener2
|
@implementation FileOpener2
|
||||||
@synthesize controller = docController;
|
|
||||||
|
|
||||||
- (void) open: (CDVInvokedUrlCommand*)command {
|
- (void) open: (CDVInvokedUrlCommand*)command {
|
||||||
|
|
||||||
NSString *path = [[command.arguments objectAtIndex:0] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
NSString *path = [command.arguments objectAtIndex:0];
|
||||||
//NSString *uti = [command.arguments objectAtIndex:1];
|
NSString *uti = nil;
|
||||||
|
if (command.arguments.count > 1) {
|
||||||
|
uti = [command.arguments objectAtIndex:1];
|
||||||
|
}
|
||||||
|
|
||||||
CDVViewController* cont = (CDVViewController*)[ super viewController ];
|
CDVViewController* cont = (CDVViewController*)[ super viewController ];
|
||||||
|
|
||||||
NSArray *dotParts = [path componentsSeparatedByString:@"."];
|
if (!uti) {
|
||||||
NSString *fileExt = [dotParts lastObject];
|
NSArray *dotParts = [path componentsSeparatedByString:@"."];
|
||||||
//NSString *fileExt = [[dotparts lastObject] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
NSString *fileExt = [dotParts lastObject];
|
||||||
|
|
||||||
NSString *uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
|
NSString *uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
//NSLog(@"path %@, uti:%@", path, uti);
|
//NSLog(@"path %@, uti:%@", path, uti);
|
||||||
NSURL *fileURL = nil;
|
NSURL *fileURL = [NSURL fileURLWithPath:path];
|
||||||
|
|
||||||
//fileURL = [NSURL URLWithString:path];
|
|
||||||
fileURL = [NSURL fileURLWithPath:path];
|
|
||||||
|
|
||||||
localFile = fileURL.path;
|
localFile = fileURL.path;
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
self.controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
|
||||||
|
self.controller.delegate = self;
|
||||||
|
self.controller.UTI = uti;
|
||||||
|
|
||||||
docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
|
CGRect rect = CGRectMake(0, 0, 1000.0f, 150.0f);
|
||||||
docController.delegate = self;
|
CDVPluginResult* pluginResult = nil;
|
||||||
docController.UTI = uti;
|
BOOL wasOpened = [docController presentOptionsMenuFromRect:rect inView:cont.view animated:NO];
|
||||||
|
|
||||||
CGRect rect = CGRectMake(0, 0, 1000.0f, 150.0f);
|
if(wasOpened) {
|
||||||
CDVPluginResult* pluginResult = nil;
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
|
||||||
BOOL wasOpened = [docController presentOptionsMenuFromRect:rect inView:cont.view animated:NO];
|
} else {
|
||||||
//presentOptionsMenuFromRect
|
NSDictionary *jsonObj = @{@"status" : @"9",
|
||||||
//presentOpenInMenuFromRect
|
@"message" : @"Could not handle UTI"};
|
||||||
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
||||||
if(wasOpened) {
|
messageAsDictionary:jsonObj];
|
||||||
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
|
}
|
||||||
//NSLog(@"Success");
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
} 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];
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user