|
| 1 | +#import "SharePluginViewController.h" |
| 2 | +#import <MobileCoreServices/MobileCoreServices.h> // For UTI |
| 3 | + |
| 4 | +@implementation SharePluginViewController |
| 5 | + |
| 6 | +@synthesize documentInteractionController, myViewController; |
| 7 | + |
| 8 | +#pragma mark - |
| 9 | +#pragma mark Utility methods |
| 10 | + |
| 11 | +- (NSString *)UTIForURL:(NSURL *)url |
| 12 | +{ |
| 13 | + CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)url.pathExtension, NULL); |
| 14 | + return (NSString *)CFBridgingRelease(UTI) ; |
| 15 | +} |
| 16 | + |
| 17 | +- (BOOL) validateFileUrl: (NSString *)candidate |
| 18 | +{ |
| 19 | + NSString* lowerCased = [candidate lowercaseString]; |
| 20 | + return [lowerCased hasPrefix:@"file://"]; |
| 21 | +} |
| 22 | + |
| 23 | +#pragma mark - |
| 24 | +#pragma mark View / Share methods |
| 25 | + |
| 26 | +- (BOOL)viewFile:(NSString *)filePath usingViewController:(UIViewController *) viewController |
| 27 | +{ |
| 28 | + self.myViewController = viewController; |
| 29 | + NSURL* URL = [NSURL URLWithString:filePath]; |
| 30 | + |
| 31 | + if (![self validateFileUrl:filePath]) { |
| 32 | + URL = [[NSURL alloc] initFileURLWithPath:filePath]; |
| 33 | + } |
| 34 | + |
| 35 | + BOOL filePreviewSuccess = NO; // Success is true if it was possible to open the controller and there are apps available |
| 36 | + |
| 37 | + if (URL) { |
| 38 | + // Initialize Document Interaction Controller |
| 39 | + self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL]; |
| 40 | + self.documentInteractionController.UTI = [self UTIForURL:URL]; |
| 41 | + self.documentInteractionController.delegate = self; |
| 42 | + |
| 43 | + UIView *filePreviewView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)]; |
| 44 | + [[self.myViewController view] addSubview:filePreviewView]; |
| 45 | + self.view = filePreviewView; |
| 46 | + |
| 47 | + // Configure Document Interaction Controller |
| 48 | + [self.documentInteractionController setDelegate:self]; |
| 49 | + |
| 50 | + // Preview |
| 51 | + filePreviewSuccess = [self.documentInteractionController presentPreviewAnimated:YES]; |
| 52 | + |
| 53 | + if(!filePreviewSuccess) { |
| 54 | + // Next, try the options menu in openFile below... |
| 55 | + return [self openFile:URL usingViewController:viewController]; |
| 56 | + } |
| 57 | + } else { |
| 58 | + return NO; |
| 59 | + } |
| 60 | + |
| 61 | + return YES; |
| 62 | +} |
| 63 | + |
| 64 | +- (BOOL) openFile:(NSURL *)URL usingViewController:(UIViewController *) viewController |
| 65 | +{ |
| 66 | + self.myViewController = viewController; |
| 67 | + |
| 68 | + BOOL fileOpenSuccess = NO; // Success is true if it was possible to open |
| 69 | + |
| 70 | + if (URL) { |
| 71 | + // Initialize Document Interaction Controller |
| 72 | + self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:URL]; |
| 73 | + self.documentInteractionController.UTI = [self UTIForURL:URL]; |
| 74 | + self.documentInteractionController.delegate = self; |
| 75 | + |
| 76 | + // Configure Document Interaction Controller |
| 77 | + [self.documentInteractionController setDelegate:self]; |
| 78 | + |
| 79 | + // Open |
| 80 | + CGRect screenRect = [[viewController view] bounds]; |
| 81 | + CGFloat screenWidth = screenRect.size.width; |
| 82 | + CGFloat screenHeight = screenRect.size.height; |
| 83 | + fileOpenSuccess = [self.documentInteractionController |
| 84 | + presentOptionsMenuFromRect:CGRectMake((screenWidth / 2), screenHeight, 1, 1) |
| 85 | + inView:[self.myViewController view] animated:YES]; |
| 86 | + |
| 87 | + if(!fileOpenSuccess) { |
| 88 | + // There is no app to handle this file |
| 89 | + NSString *deviceType = [UIDevice currentDevice].localizedModel; |
| 90 | + NSString *message = [NSString stringWithFormat:NSLocalizedString( |
| 91 | + @"Your %@ doesn't seem to have any other Apps installed that can open this document.", |
| 92 | + @"Your %@ doesn't seem to have any other Apps installed that can open this document."), |
| 93 | + deviceType]; |
| 94 | + |
| 95 | + // Display alert |
| 96 | + UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString( |
| 97 | + @"No suitable Apps installed", @"No suitable App installed") |
| 98 | + message:message |
| 99 | + delegate:nil |
| 100 | + cancelButtonTitle:NSLocalizedString(@"OK", @"OK") |
| 101 | + otherButtonTitles:nil]; |
| 102 | + [alert show]; |
| 103 | + return NO; |
| 104 | + } |
| 105 | + } else { |
| 106 | + return NO; |
| 107 | + } |
| 108 | + |
| 109 | + return YES; |
| 110 | +} |
| 111 | + |
| 112 | +#pragma mark - |
| 113 | +#pragma mark Document Interaction Controller Delegate Methods |
| 114 | +- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller { |
| 115 | + return self.myViewController; |
| 116 | +} |
| 117 | + |
| 118 | +@end |
0 commit comments