FileOpener2.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2013 pwlin - pwlin05@gmail.com
  4. Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. this software and associated documentation files (the "Software"), to deal in
  6. the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. the Software, and to permit persons to whom the Software is furnished to do so,
  9. subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. */
  19. #import "FileOpener2.h"
  20. #import <Cordova/CDV.h>
  21. #import <QuartzCore/QuartzCore.h>
  22. #import <MobileCoreServices/MobileCoreServices.h>
  23. @implementation FileOpener2
  24. @synthesize controller = docController;
  25. CDVPluginResult* pluginResult = nil;
  26. NSString* callbackId = nil;
  27. - (void) open: (CDVInvokedUrlCommand*)command {
  28. callbackId = command.callbackId;
  29. NSString *path = [command.arguments objectAtIndex:0];
  30. NSString *contentType = [command.arguments objectAtIndex:1];
  31. BOOL showPreview = YES;
  32. if ([command.arguments count] >= 3) {
  33. showPreview = [[command.arguments objectAtIndex:2] boolValue];
  34. }
  35. CDVViewController* cont = (CDVViewController*)[super viewController];
  36. self.cdvViewController = cont;
  37. NSString *uti = nil;
  38. if ([contentType length] == 0) {
  39. NSArray *dotParts = [path componentsSeparatedByString:@"."];
  40. NSString *fileExt = [dotParts lastObject];
  41. uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
  42. } else {
  43. uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)contentType, NULL);
  44. }
  45. dispatch_async(dispatch_get_main_queue(), ^{
  46. NSURL *fileURL = NULL;
  47. NSString *decodedPath = [path stringByRemovingPercentEncoding];
  48. if ([path isEqualToString:decodedPath]) {
  49. NSLog(@"Path parameter not encoded. Building file URL encoding it...");
  50. fileURL = [NSURL fileURLWithPath:[path stringByReplacingOccurrencesOfString:@"file://" withString:@""]];;
  51. } else {
  52. NSLog(@"Path parameter already encoded. Building file URL without encoding it...");
  53. fileURL = [NSURL URLWithString:path];
  54. }
  55. localFile = fileURL.path;
  56. NSLog(@"looking for file at %@", fileURL);
  57. NSFileManager *fm = [NSFileManager defaultManager];
  58. if (![fm fileExistsAtPath:localFile]) {
  59. NSDictionary *jsonObj = @{@"status" : @"9",
  60. @"message" : @"File does not exist"};
  61. CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
  62. [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
  63. return;
  64. }
  65. docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
  66. docController.delegate = self;
  67. docController.UTI = uti;
  68. //Opens the file preview
  69. CGRect rect;
  70. if ([command.arguments count] >= 4) {
  71. NSArray *positionValues = [command.arguments objectAtIndex:3];
  72. if (![positionValues isEqual:[NSNull null]] && [positionValues count] >= 2) {
  73. rect = CGRectMake(0, 0, [[positionValues objectAtIndex:0] floatValue], [[positionValues objectAtIndex:1] floatValue]);
  74. } else {
  75. rect = CGRectMake(0, 0, 0, 0);
  76. }
  77. } else {
  78. rect = CGRectMake(0, 0, cont.view.bounds.size.width, cont.view.bounds.size.height);
  79. }
  80. BOOL wasOpened = NO;
  81. if (showPreview) {
  82. wasOpened = [docController presentPreviewAnimated: NO];
  83. } else {
  84. CDVViewController* cont = self.cdvViewController;
  85. wasOpened = [docController presentOpenInMenuFromRect:rect inView:cont.view animated:YES];
  86. }
  87. if (wasOpened) {
  88. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
  89. } else {
  90. NSDictionary *jsonObj = [ [NSDictionary alloc]
  91. initWithObjectsAndKeys :
  92. @"9", @"status",
  93. @"Could not handle UTI", @"message",
  94. nil
  95. ];
  96. pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
  97. [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
  98. }
  99. });
  100. }
  101. @end
  102. @implementation FileOpener2 (UIDocumentInteractionControllerDelegate)
  103. - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
  104. [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
  105. }
  106. - (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
  107. [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
  108. }
  109. - (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
  110. UIViewController *presentingViewController = self.viewController;
  111. if (presentingViewController.view.window != [UIApplication sharedApplication].keyWindow) {
  112. presentingViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
  113. }
  114. while (presentingViewController.presentedViewController != nil && ![presentingViewController.presentedViewController isBeingDismissed]) {
  115. presentingViewController = presentingViewController.presentedViewController;
  116. }
  117. return presentingViewController;
  118. }
  119. @end