Skip to content

fix(share_plus): Present Share Sheet on the top ViewController #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/share_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.1.3

- Update iOS share target to present on the top ViewController. This fixes "Unable to present" errors when the app is already presenting such as in an add to app scenario.

# 2.1.2

- Do not tear down method channel onDetachedFromActivity.
Expand Down
23 changes: 21 additions & 2 deletions packages/share_plus/ios/Classes/FLTSharePlusPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@

static NSString *const PLATFORM_CHANNEL = @"dev.fluttercommunity.plus/share";

static UIViewController *RootViewController() {
return [UIApplication sharedApplication].keyWindow.rootViewController;
}

static UIViewController *TopViewControllerForViewController(UIViewController *viewController) {
if (viewController.presentedViewController) {
return TopViewControllerForViewController(viewController.presentedViewController);
}
if ([viewController isKindOfClass:[UINavigationController class]]) {
return TopViewControllerForViewController(
((UINavigationController *)viewController).visibleViewController);
}
return viewController;
}

@interface SharePlusData : NSObject <UIActivityItemSource>

@property(readonly, nonatomic, copy) NSString *subject;
Expand Down Expand Up @@ -135,9 +150,11 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
return;
}

UIViewController *topViewController =
TopViewControllerForViewController(RootViewController());
[self shareText:shareText
subject:shareSubject
withController:[UIApplication sharedApplication].keyWindow.rootViewController
withController:topViewController
atSource:originRect];
result(nil);
} else if ([@"shareFiles" isEqualToString:call.method]) {
Expand All @@ -162,11 +179,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
}
}

UIViewController *topViewController =
TopViewControllerForViewController(RootViewController());
[self shareFiles:paths
withMimeType:mimeTypes
withSubject:subject
withText:text
withController:[UIApplication sharedApplication].keyWindow.rootViewController
withController:topViewController
atSource:originRect];
result(nil);
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/share_plus/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: share_plus
description: Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityViewController on iOS.
version: 2.1.2
version: 2.1.3
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/

Expand Down