Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,27 @@ @interface OpenFilePlugin ()<UIDocumentInteractionControllerDelegate>

static NSString *const CHANNEL_NAME = @"open_file";

static UIViewController *RootViewController(void) {
if (@available(iOS 13, *)) { // UIApplication.keyWindow is deprecated
NSSet *scenes = [[UIApplication sharedApplication] connectedScenes];
for (UIScene *scene in scenes) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
NSArray *windows = ((UIWindowScene *)scene).windows;
for (UIWindow *window in windows) {
if (window.isKeyWindow) {
return window.rootViewController;
}
}
}
}
return nil;
} else {
return [UIApplication sharedApplication].delegate.window.rootViewController;
}
}

@implementation OpenFilePlugin{
FlutterResult _result;
UIViewController *_viewController;
UIDocumentInteractionController *_documentController;
UIDocumentInteractionController *_interactionController;
}
Expand All @@ -16,20 +34,10 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterMethodChannel* channel = [FlutterMethodChannel
methodChannelWithName:CHANNEL_NAME
binaryMessenger:[registrar messenger]];
UIViewController *viewController =
[UIApplication sharedApplication].delegate.window.rootViewController;
OpenFilePlugin* instance = [[OpenFilePlugin alloc] initWithViewController:viewController];
OpenFilePlugin* instance = [[OpenFilePlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:channel];
}

- (instancetype)initWithViewController:(UIViewController *)viewController {
self = [super init];
if (self) {
_viewController = viewController;
}
return self;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"open_file" isEqualToString:call.method]) {
_result = result;
Expand All @@ -49,14 +57,22 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
_documentController.delegate = self;
BOOL isAppOpen = [call.arguments[@"isIOSAppOpen"] boolValue];
@try {
UIViewController *rootViewController = RootViewController();
if (!rootViewController) {
NSDictionary * dict = @{@"message":@"the root view controller could not be found", @"type":@-4};
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
NSString * json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
result(json);
return;
}
if (isAppOpen) {
[self openFileWithUIActivityViewController:fileURL];
[self openFileWithUIActivityViewController:fileURL vc:rootViewController];
}else{
BOOL previewSucceeded = [_documentController presentPreviewAnimated:YES];
if(!previewSucceeded){
// [_documentController presentOpenInMenuFromRect:CGRectMake(500,20,100,100) inView:[UIApplication sharedApplication].delegate.window.rootViewController.view animated:YES];

[self openFileWithUIActivityViewController:fileURL];
[self openFileWithUIActivityViewController:fileURL vc:rootViewController];
}
}

Expand All @@ -73,16 +89,16 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}
}

- (void)openFileWithUIActivityViewController:(NSURL *)fileURL{
- (void)openFileWithUIActivityViewController:(NSURL *)fileURL vc:(UIViewController *)rootViewController {
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[fileURL] applicationActivities:nil];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
activityViewController.popoverPresentationController.sourceView = _viewController.view;
activityViewController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(_viewController.view.bounds), CGRectGetMidY(_viewController.view.bounds), 0, 0);
activityViewController.popoverPresentationController.sourceView = rootViewController.view;
activityViewController.popoverPresentationController.sourceRect = CGRectMake(CGRectGetMidX(rootViewController.view.bounds), CGRectGetMidY(rootViewController.view.bounds), 0, 0);
activityViewController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
}

[_viewController presentViewController:activityViewController animated:YES completion:^{ [self doneEnd];}];
[rootViewController presentViewController:activityViewController animated:YES completion:^{ [self doneEnd];}];
}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
Expand All @@ -94,7 +110,7 @@ - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteraction
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return [UIApplication sharedApplication].delegate.window.rootViewController;
return RootViewController();
}

- (BOOL) isBlankString:(NSString *)string {
Expand Down