Skip to content

Commit 1240b62

Browse files
fix(share_plus): Present Share Sheet on the top ViewController (continuation) (#307)
* Present on Top ViewController - Present share sheet on the currently visible view controller. * Update CHANGELOG * Update Share v2.1.3 Pubspec * format share_plus ios code Co-authored-by: Ray Li <[email protected]> Co-authored-by: Miquel Beltran <[email protected]>
1 parent fc6864f commit 1240b62

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

packages/share_plus/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 2.1.3
2+
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.
4+
15
# 2.1.2
26

37
- Do not tear down method channel onDetachedFromActivity.

packages/share_plus/ios/Classes/FLTSharePlusPlugin.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77

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

10+
static UIViewController *RootViewController() {
11+
return [UIApplication sharedApplication].keyWindow.rootViewController;
12+
}
13+
14+
static UIViewController *TopViewControllerForViewController(UIViewController *viewController) {
15+
if (viewController.presentedViewController) {
16+
return TopViewControllerForViewController(viewController.presentedViewController);
17+
}
18+
if ([viewController isKindOfClass:[UINavigationController class]]) {
19+
return TopViewControllerForViewController(
20+
((UINavigationController *)viewController).visibleViewController);
21+
}
22+
return viewController;
23+
}
24+
1025
@interface SharePlusData : NSObject <UIActivityItemSource>
1126

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

153+
UIViewController *topViewController =
154+
TopViewControllerForViewController(RootViewController());
138155
[self shareText:shareText
139156
subject:shareSubject
140-
withController:[UIApplication sharedApplication].keyWindow.rootViewController
157+
withController:topViewController
141158
atSource:originRect];
142159
result(nil);
143160
} else if ([@"shareFiles" isEqualToString:call.method]) {
@@ -162,11 +179,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
162179
}
163180
}
164181

182+
UIViewController *topViewController =
183+
TopViewControllerForViewController(RootViewController());
165184
[self shareFiles:paths
166185
withMimeType:mimeTypes
167186
withSubject:subject
168187
withText:text
169-
withController:[UIApplication sharedApplication].keyWindow.rootViewController
188+
withController:topViewController
170189
atSource:originRect];
171190
result(nil);
172191
} else {

packages/share_plus/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: share_plus
22
description: Flutter plugin for sharing content via the platform share UI, using the ACTION_SEND intent on Android and UIActivityViewController on iOS.
3-
version: 2.1.2
3+
version: 2.1.3
44
homepage: https://plus.fluttercommunity.dev/
55
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
66

0 commit comments

Comments
 (0)