Skip to content

fix(share_plus): Throw error if sharePositionOrigin not in sourceView #1038

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

Merged
merged 6 commits into from
Sep 20, 2022
Merged
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/share_plus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.3.0

- iOS: Throw PlatformException when iPad share dialog not appearing (sharePositionOrigin not in sourceView)

# 4.2.0

- iOS: Fix Instagram does not show up in provider list for web links
Expand Down
55 changes: 44 additions & 11 deletions packages/share_plus/share_plus/ios/Classes/FLTSharePlusPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {

UIViewController *topViewController =
TopViewControllerForViewController(RootViewController());

[self shareText:shareText
subject:shareSubject
withController:topViewController
atSource:originRect
toResult:withResult ? result : nil];
toResult:result
withResult:withResult];
if (!withResult)
result(nil);
} else if ([@"shareFiles" isEqualToString:call.method] ||
Expand Down Expand Up @@ -336,7 +338,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
withText:text
withController:topViewController
atSource:originRect
toResult:withResult ? result : nil];
toResult:result
withResult:withResult];
if (!withResult)
result(nil);
} else {
Expand All @@ -349,7 +352,8 @@ + (void)share:(NSArray *)shareItems
withSubject:(NSString *)subject
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
toResult:(FlutterResult)result
withResult:(BOOL)withResult {
UIActivityViewSuccessController *activityViewController =
[[UIActivityViewSuccessController alloc] initWithActivityItems:shareItems
applicationActivities:nil];
Expand All @@ -361,10 +365,32 @@ + (void)share:(NSArray *)shareItems

activityViewController.popoverPresentationController.sourceView =
controller.view;
BOOL isCoordinateSpaceOfSourceView =
CGRectContainsRect(controller.view.frame, origin);

// If device is e.g. an iPad then hasPopoverPresentationController is true
BOOL hasPopoverPresentationController =
[activityViewController popoverPresentationController] != NULL;
if (hasPopoverPresentationController &&
(!isCoordinateSpaceOfSourceView || CGRectIsEmpty(origin))) {
NSString *sharePositionIssue = [NSString
stringWithFormat:
@"sharePositionOrigin: argument must be set, %@ must be non-zero "
@"and within coordinate space of source view: %@",
NSStringFromCGRect(origin),
NSStringFromCGRect(controller.view.bounds)];

result([FlutterError errorWithCode:@"error"
message:sharePositionIssue
details:nil]);
return;
}

if (!CGRectIsEmpty(origin)) {
activityViewController.popoverPresentationController.sourceRect = origin;
}
if (result) {

if (withResult) {
UIActivityViewSuccessCompanion *companion =
[[UIActivityViewSuccessCompanion alloc] initWithResult:result];
activityViewController.companion = companion;
Expand All @@ -384,7 +410,8 @@ + (void)shareText:(NSString *)shareText
subject:(NSString *)subject
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
toResult:(FlutterResult)result
withResult:(BOOL)withResult {
NSURL *url = [NSURL URLWithString:shareText];
if (!url || ![url scheme] || ![url host]) {
NSObject *data = [[SharePlusData alloc] initWithSubject:subject
Expand All @@ -393,7 +420,8 @@ + (void)shareText:(NSString *)shareText
withSubject:subject
withController:controller
atSource:origin
toResult:result];
toResult:result
withResult:withResult];
return;
}

Expand All @@ -415,7 +443,8 @@ + (void)shareText:(NSString *)shareText
withSubject:subject
withController:controller
atSource:origin
toResult:result];
toResult:result
withResult:withResult];
});

return;
Expand All @@ -430,7 +459,8 @@ + (void)shareText:(NSString *)shareText
withSubject:subject
withController:controller
atSource:origin
toResult:result];
toResult:result
withResult:withResult];
});
}];
} else {
Expand All @@ -439,7 +469,8 @@ + (void)shareText:(NSString *)shareText
withSubject:subject
withController:controller
atSource:origin
toResult:result];
toResult:result
withResult:withResult];
return;
}
}
Expand All @@ -450,7 +481,8 @@ + (void)shareFiles:(NSArray *)paths
withText:(NSString *)text
withController:(UIViewController *)controller
atSource:(CGRect)origin
toResult:(FlutterResult)result {
toResult:(FlutterResult)result
withResult:(BOOL)withResult {
NSMutableArray *items = [[NSMutableArray alloc] init];

for (int i = 0; i < [paths count]; i++) {
Expand All @@ -469,7 +501,8 @@ + (void)shareFiles:(NSArray *)paths
withSubject:subject
withController:controller
atSource:origin
toResult:result];
toResult:result
withResult:withResult];
}

@end
2 changes: 1 addition & 1 deletion packages/share_plus/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: 4.2.0
version: 4.3.0
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/
issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/share_plus
Expand Down