Skip to content

Commit db78ed0

Browse files
committed
New Share / open in methods
Change iOS share to use `presentOptionsMenuFromRect:inView:animated:` instead of an `UIActivityViewController` View also falls back to `presentOptionsMenuFromRect:inView:animated:` if the file cannot be previewed. Fixes SpiderOak/SpiderOakMobileClient#448 and others (at least on iOS)
1 parent b272612 commit db78ed0

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

src/ios/FileViewerPlugin.m

+14-8
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,14 @@ - (void)view:(CDVInvokedUrlCommand*)command
2222
- (void)hide:(CDVInvokedUrlCommand*)command
2323
{
2424
[self.previewViewController.documentInteractionController dismissPreviewAnimated:YES];
25-
[self.activityViewController dismissModalViewControllerAnimated:YES];
25+
[self.previewViewController.documentInteractionController dismissMenuAnimated:YES];
26+
[self.activityViewController dismissViewControllerAnimated:YES completion:nil];
2627
}
2728

2829
- (void)share:(CDVInvokedUrlCommand*)command
2930
{
31+
BOOL pluginSuccess = NO;
32+
3033
NSDictionary* arguments = [command.arguments objectAtIndex:0];
3134
NSString* filePath = [arguments objectForKey:@"url"];
3235
NSString *shareString = @"";
@@ -45,13 +48,16 @@ - (void)share:(CDVInvokedUrlCommand*)command
4548
}
4649
}
4750

48-
NSArray *activityItems = [NSArray arrayWithObjects:shareString, fileUrl, nil];
49-
50-
self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
51-
self.activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
52-
self.activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
53-
54-
[[super viewController] presentViewController:self.activityViewController animated:YES completion:nil];
51+
if (shareString.length != 0) {
52+
NSArray *activityItems = [NSArray arrayWithObjects:shareString, fileUrl, nil];
53+
54+
self.activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
55+
self.activityViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
56+
[[super viewController] presentViewController:self.activityViewController animated:YES completion:nil];
57+
} else {
58+
self.previewViewController = [[FileViewerPluginViewController alloc] init];
59+
pluginSuccess = [self.previewViewController openFile:fileUrl usingViewController:[super viewController]];
60+
}
5561
}
5662

5763
@end

src/ios/FileViewerPluginViewController.h

+1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
@property (retain, nonatomic) UIViewController* myViewController;
99

1010
- (BOOL)viewFile:(NSString *)filePath usingViewController: (UIViewController *) viewController;
11+
- (BOOL)openFile:(NSURL *)URL usingViewController: (UIViewController *) viewController;
1112

1213
@end

src/ios/FileViewerPluginViewController.m

+50-14
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,12 @@ - (BOOL)viewFile:(NSString *)filePath usingViewController:(UIViewController *) v
4747
// Configure Document Interaction Controller
4848
[self.documentInteractionController setDelegate:self];
4949

50-
// Preview PDF
50+
// Preview
5151
filePreviewSuccess = [self.documentInteractionController presentPreviewAnimated:YES];
5252

5353
if(!filePreviewSuccess) {
54-
// There is no app to handle this file
55-
NSString *deviceType = [UIDevice currentDevice].localizedModel;
56-
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Your %@ doesn't seem to have any other Apps installed that can open this document.",
57-
@"Your %@ doesn't seem to have any other Apps installed that can open this document."), deviceType];
58-
59-
// Display alert
60-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No suitable Apps installed", @"No suitable App installed")
61-
message:message
62-
delegate:nil
63-
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
64-
otherButtonTitles:nil];
65-
[alert show];
66-
return NO;
54+
// Next, try the options menu in openFile below...
55+
return [self openFile:URL usingViewController:viewController];
6756
}
6857
} else {
6958
return NO;
@@ -72,6 +61,53 @@ - (BOOL)viewFile:(NSString *)filePath usingViewController:(UIViewController *) v
7261
return YES;
7362
}
7463

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+
UIView *fileOpenView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
77+
[[self.myViewController view] addSubview:fileOpenView];
78+
self.view = fileOpenView;
79+
80+
// Configure Document Interaction Controller
81+
[self.documentInteractionController setDelegate:self];
82+
83+
// Open
84+
CGRect screenRect = [[viewController view] bounds];
85+
CGFloat screenWidth = screenRect.size.width;
86+
CGFloat screenHeight = screenRect.size.height;
87+
fileOpenSuccess = [self.documentInteractionController presentOptionsMenuFromRect:CGRectMake((screenWidth / 2), screenHeight, 1, 1) inView:fileOpenView animated:YES];
88+
89+
if(!fileOpenSuccess) {
90+
// There is no app to handle this file
91+
NSString *deviceType = [UIDevice currentDevice].localizedModel;
92+
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Your %@ doesn't seem to have any other Apps installed that can open this document.",
93+
@"Your %@ doesn't seem to have any other Apps installed that can open this document."), deviceType];
94+
95+
// Display alert
96+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No suitable Apps installed", @"No suitable App installed")
97+
message:message
98+
delegate:nil
99+
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
100+
otherButtonTitles:nil];
101+
[alert show];
102+
return NO;
103+
}
104+
} else {
105+
return NO;
106+
}
107+
108+
return YES;
109+
}
110+
75111
#pragma mark -
76112
#pragma mark Document Interaction Controller Delegate Methods
77113
- (UIViewController *) documentInteractionControllerViewControllerForPreview: (UIDocumentInteractionController *) controller {

0 commit comments

Comments
 (0)