@@ -298,11 +298,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
298
298
299
299
UIViewController *topViewController =
300
300
TopViewControllerForViewController (RootViewController ());
301
+
301
302
[self shareText: shareText
302
303
subject: shareSubject
303
304
withController: topViewController
304
305
atSource: originRect
305
- toResult: withResult ? result : nil ];
306
+ toResult: result
307
+ withResult: withResult];
306
308
if (!withResult)
307
309
result (nil );
308
310
} else if ([@" shareFiles" isEqualToString: call.method] ||
@@ -336,7 +338,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
336
338
withText: text
337
339
withController: topViewController
338
340
atSource: originRect
339
- toResult: withResult ? result : nil ];
341
+ toResult: result
342
+ withResult: withResult];
340
343
if (!withResult)
341
344
result (nil );
342
345
} else {
@@ -349,7 +352,8 @@ + (void)share:(NSArray *)shareItems
349
352
withSubject : (NSString *)subject
350
353
withController : (UIViewController *)controller
351
354
atSource : (CGRect )origin
352
- toResult : (FlutterResult)result {
355
+ toResult : (FlutterResult)result
356
+ withResult : (BOOL )withResult {
353
357
UIActivityViewSuccessController *activityViewController =
354
358
[[UIActivityViewSuccessController alloc ] initWithActivityItems: shareItems
355
359
applicationActivities: nil ];
@@ -361,10 +365,32 @@ + (void)share:(NSArray *)shareItems
361
365
362
366
activityViewController.popoverPresentationController .sourceView =
363
367
controller.view ;
368
+ BOOL isCoordinateSpaceOfSourceView =
369
+ CGRectContainsRect (controller.view .frame , origin);
370
+
371
+ // If device is e.g. an iPad then hasPopoverPresentationController is true
372
+ BOOL hasPopoverPresentationController =
373
+ [activityViewController popoverPresentationController ] != NULL ;
374
+ if (hasPopoverPresentationController &&
375
+ (!isCoordinateSpaceOfSourceView || CGRectIsEmpty (origin))) {
376
+ NSString *sharePositionIssue = [NSString
377
+ stringWithFormat:
378
+ @" sharePositionOrigin: argument must be set, %@ must be non-zero "
379
+ @" and within coordinate space of source view: %@ " ,
380
+ NSStringFromCGRect (origin),
381
+ NSStringFromCGRect(controller.view.bounds)];
382
+
383
+ result ([FlutterError errorWithCode: @" error"
384
+ message: sharePositionIssue
385
+ details: nil ]);
386
+ return ;
387
+ }
388
+
364
389
if (!CGRectIsEmpty (origin)) {
365
390
activityViewController.popoverPresentationController .sourceRect = origin;
366
391
}
367
- if (result) {
392
+
393
+ if (withResult) {
368
394
UIActivityViewSuccessCompanion *companion =
369
395
[[UIActivityViewSuccessCompanion alloc ] initWithResult: result];
370
396
activityViewController.companion = companion;
@@ -384,7 +410,8 @@ + (void)shareText:(NSString *)shareText
384
410
subject: (NSString *)subject
385
411
withController: (UIViewController *)controller
386
412
atSource: (CGRect )origin
387
- toResult : (FlutterResult)result {
413
+ toResult: (FlutterResult)result
414
+ withResult: (BOOL )withResult {
388
415
NSURL *url = [NSURL URLWithString: shareText];
389
416
if (!url || ![url scheme ] || ![url host ]) {
390
417
NSObject *data = [[SharePlusData alloc ] initWithSubject: subject
@@ -393,7 +420,8 @@ + (void)shareText:(NSString *)shareText
393
420
withSubject: subject
394
421
withController: controller
395
422
atSource: origin
396
- toResult: result];
423
+ toResult: result
424
+ withResult: withResult];
397
425
return ;
398
426
}
399
427
@@ -415,7 +443,8 @@ + (void)shareText:(NSString *)shareText
415
443
withSubject: subject
416
444
withController: controller
417
445
atSource: origin
418
- toResult: result];
446
+ toResult: result
447
+ withResult: withResult];
419
448
});
420
449
421
450
return ;
@@ -430,7 +459,8 @@ + (void)shareText:(NSString *)shareText
430
459
withSubject: subject
431
460
withController: controller
432
461
atSource: origin
433
- toResult: result];
462
+ toResult: result
463
+ withResult: withResult];
434
464
});
435
465
}];
436
466
} else {
@@ -439,7 +469,8 @@ + (void)shareText:(NSString *)shareText
439
469
withSubject: subject
440
470
withController: controller
441
471
atSource: origin
442
- toResult: result];
472
+ toResult: result
473
+ withResult: withResult];
443
474
return ;
444
475
}
445
476
}
@@ -450,7 +481,8 @@ + (void)shareFiles:(NSArray *)paths
450
481
withText: (NSString *)text
451
482
withController: (UIViewController *)controller
452
483
atSource: (CGRect )origin
453
- toResult : (FlutterResult)result {
484
+ toResult: (FlutterResult)result
485
+ withResult: (BOOL )withResult {
454
486
NSMutableArray *items = [[NSMutableArray alloc ] init ];
455
487
456
488
for (int i = 0 ; i < [paths count ]; i++) {
@@ -469,7 +501,8 @@ + (void)shareFiles:(NSArray *)paths
469
501
withSubject: subject
470
502
withController: controller
471
503
atSource: origin
472
- toResult: result];
504
+ toResult: result
505
+ withResult: withResult];
473
506
}
474
507
475
508
@end
0 commit comments