Skip to content

Commit 7ecda36

Browse files
DependencyBottkreuder
DependencyBot
authored andcommitted
Ensure that it raises a PlatformException if argument is required, but not given
1 parent b56876f commit 7ecda36

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

packages/share_plus/share_plus/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 4.2.0
22

33
- iOS: Throw PlatformException when iPad share dialog not appearing (sharePositionOrigin not in sourceView)
4+
45
## 4.1.0
56

67
- iOS: Fix text sharing.

packages/share_plus/share_plus/ios/Classes/FLTSharePlusPlugin.m

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,27 +261,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
261261

262262
UIViewController *topViewController =
263263
TopViewControllerForViewController(RootViewController());
264-
BOOL isCoordinateSpaceOfSourceView =
265-
CGRectContainsRect(topViewController.view.frame, originRect);
266-
if (!isCoordinateSpaceOfSourceView) {
267-
result([FlutterError
268-
errorWithCode:@"error"
269-
message:[NSString
270-
stringWithFormat:
271-
@"sharePositionOrigin: %@ must be within "
272-
@"coordinate space of source view: %@",
273-
NSStringFromCGRect(originRect),
274-
NSStringFromCGRect(
275-
topViewController.view.bounds)]
276-
details:nil]);
277-
return;
278-
}
279264

280265
[self shareText:shareText
281266
subject:shareSubject
282267
withController:topViewController
283268
atSource:originRect
284-
toResult:withResult ? result : nil];
269+
toResult:result
270+
withResult:withResult];
285271
if (!withResult)
286272
result(nil);
287273
} else if ([@"shareFiles" isEqualToString:call.method] ||
@@ -315,7 +301,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
315301
withText:text
316302
withController:topViewController
317303
atSource:originRect
318-
toResult:withResult ? result : nil];
304+
toResult:result
305+
withResult:withResult];
319306
if (!withResult)
320307
result(nil);
321308
} else {
@@ -328,7 +315,8 @@ + (void)share:(NSArray *)shareItems
328315
withSubject:(NSString *)subject
329316
withController:(UIViewController *)controller
330317
atSource:(CGRect)origin
331-
toResult:(FlutterResult)result {
318+
toResult:(FlutterResult)result
319+
withResult:(BOOL)withResult {
332320
UIActivityViewSuccessController *activityViewController =
333321
[[UIActivityViewSuccessController alloc] initWithActivityItems:shareItems
334322
applicationActivities:nil];
@@ -340,10 +328,32 @@ + (void)share:(NSArray *)shareItems
340328

341329
activityViewController.popoverPresentationController.sourceView =
342330
controller.view;
331+
BOOL isCoordinateSpaceOfSourceView =
332+
CGRectContainsRect(controller.view.frame, origin);
333+
334+
// If device is e.g. an iPad then hasPopoverPresentationController is true
335+
BOOL hasPopoverPresentationController =
336+
[activityViewController popoverPresentationController] != NULL;
337+
if (hasPopoverPresentationController &&
338+
(!isCoordinateSpaceOfSourceView || CGRectIsEmpty(origin))) {
339+
NSString *sharePositionIssue = [NSString
340+
stringWithFormat:
341+
@"sharePositionOrigin: argument must be set, %@ must be non-zero "
342+
@"and within coordinate space of source view: %@",
343+
NSStringFromCGRect(origin),
344+
NSStringFromCGRect(controller.view.bounds)];
345+
346+
result([FlutterError errorWithCode:@"error"
347+
message:sharePositionIssue
348+
details:nil]);
349+
return;
350+
}
351+
343352
if (!CGRectIsEmpty(origin)) {
344353
activityViewController.popoverPresentationController.sourceRect = origin;
345354
}
346-
if (result) {
355+
356+
if (withResult) {
347357
UIActivityViewSuccessCompanion *companion =
348358
[[UIActivityViewSuccessCompanion alloc] initWithResult:result];
349359
activityViewController.companion = companion;
@@ -363,14 +373,16 @@ + (void)shareText:(NSString *)shareText
363373
subject:(NSString *)subject
364374
withController:(UIViewController *)controller
365375
atSource:(CGRect)origin
366-
toResult:(FlutterResult)result {
367-
NSObject *data = [[SharePlusData alloc] initWithSubject:subject
376+
toResult:(FlutterResult)result
377+
withResult:(BOOL)withResult {
378+
NSObject *data = [[SharePlusData alloc] initWithSubject:subject
368379
text:shareText];
369380
[self share:@[ data ]
370381
withSubject:subject
371382
withController:controller
372383
atSource:origin
373-
toResult:result];
384+
toResult:result
385+
withResult:withResult];
374386
}
375387

376388
+ (void)shareFiles:(NSArray *)paths
@@ -379,7 +391,8 @@ + (void)shareFiles:(NSArray *)paths
379391
withText:(NSString *)text
380392
withController:(UIViewController *)controller
381393
atSource:(CGRect)origin
382-
toResult:(FlutterResult)result {
394+
toResult:(FlutterResult)result
395+
withResult:(BOOL)withResult {
383396
NSMutableArray *items = [[NSMutableArray alloc] init];
384397

385398
for (int i = 0; i < [paths count]; i++) {
@@ -398,7 +411,8 @@ + (void)shareFiles:(NSArray *)paths
398411
withSubject:subject
399412
withController:controller
400413
atSource:origin
401-
toResult:result];
414+
toResult:result
415+
withResult:withResult];
402416
}
403417

404418
@end

0 commit comments

Comments
 (0)