mirror of
https://gitee.com/shuto/cordova-plugin-file-opener2.git
synced 2025-01-31 12:02:49 +08:00
Merge pull request #50 from SimpleHQ/ios-url-support
updated fix URL encoding errors on iOS
This commit is contained in:
commit
352c3b3072
@ -27,57 +27,54 @@ 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[0];
|
||||||
//NSString *uti = [command.arguments objectAtIndex:1];
|
NSString *uti = command.arguments[1];
|
||||||
|
if (!uti || (NSNull*)uti == [NSNull null]) {
|
||||||
|
NSArray *dotParts = [path componentsSeparatedByString:@"."];
|
||||||
|
NSString *fileExt = [dotParts lastObject];
|
||||||
|
|
||||||
|
uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
CDVViewController* cont = (CDVViewController*)[ super viewController ];
|
CDVViewController* cont = (CDVViewController*)[ super viewController ];
|
||||||
|
|
||||||
NSArray *dotParts = [path componentsSeparatedByString:@"."];
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
NSString *fileExt = [dotParts lastObject];
|
// TODO: test if this is a URI or a path
|
||||||
//NSString *fileExt = [[dotparts lastObject] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
|
NSURL *fileURL = [NSURL URLWithString:path];
|
||||||
|
|
||||||
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];
|
|
||||||
fileURL = [NSURL URLWithString:path];
|
|
||||||
//NSLog(@"%@",fileURL);
|
|
||||||
localFile = fileURL.path;
|
localFile = fileURL.path;
|
||||||
|
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
NSLog(@"looking for file at %@", fileURL);
|
||||||
|
NSFileManager *fm = [NSFileManager defaultManager];
|
||||||
docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
|
if(![fm fileExistsAtPath:localFile]) {
|
||||||
docController.delegate = self;
|
NSDictionary *jsonObj = @{@"status" : @"9",
|
||||||
docController.UTI = uti;
|
@"message" : @"File does not exist"};
|
||||||
|
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
||||||
CGRect rect = CGRectMake(0, 0, 1000.0f, 150.0f);
|
messageAsDictionary:jsonObj];
|
||||||
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];
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
});
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.controller = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
|
||||||
|
self.controller.delegate = self;
|
||||||
|
self.controller.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];
|
||||||
|
|
||||||
|
if(wasOpened) {
|
||||||
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
|
||||||
|
} else {
|
||||||
|
NSDictionary *jsonObj = @{@"status" : @"9",
|
||||||
|
@"message" : @"Could not handle UTI"};
|
||||||
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
||||||
|
messageAsDictionary:jsonObj];
|
||||||
|
}
|
||||||
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user