@@ -261,27 +261,13 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
261
261
262
262
UIViewController *topViewController =
263
263
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
- }
279
264
280
265
[self shareText: shareText
281
266
subject: shareSubject
282
267
withController: topViewController
283
268
atSource: originRect
284
- toResult: withResult ? result : nil ];
269
+ toResult: result
270
+ withResult: withResult];
285
271
if (!withResult)
286
272
result (nil );
287
273
} else if ([@" shareFiles" isEqualToString: call.method] ||
@@ -315,7 +301,8 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
315
301
withText: text
316
302
withController: topViewController
317
303
atSource: originRect
318
- toResult: withResult ? result : nil ];
304
+ toResult: result
305
+ withResult: withResult];
319
306
if (!withResult)
320
307
result (nil );
321
308
} else {
@@ -328,7 +315,8 @@ + (void)share:(NSArray *)shareItems
328
315
withSubject : (NSString *)subject
329
316
withController : (UIViewController *)controller
330
317
atSource : (CGRect )origin
331
- toResult: (FlutterResult)result {
318
+ toResult : (FlutterResult)result
319
+ withResult : (BOOL )withResult {
332
320
UIActivityViewSuccessController *activityViewController =
333
321
[[UIActivityViewSuccessController alloc ] initWithActivityItems: shareItems
334
322
applicationActivities: nil ];
@@ -340,10 +328,32 @@ + (void)share:(NSArray *)shareItems
340
328
341
329
activityViewController.popoverPresentationController .sourceView =
342
330
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
+
343
352
if (!CGRectIsEmpty (origin)) {
344
353
activityViewController.popoverPresentationController .sourceRect = origin;
345
354
}
346
- if (result) {
355
+
356
+ if (withResult) {
347
357
UIActivityViewSuccessCompanion *companion =
348
358
[[UIActivityViewSuccessCompanion alloc ] initWithResult: result];
349
359
activityViewController.companion = companion;
@@ -363,7 +373,8 @@ + (void)shareText:(NSString *)shareText
363
373
subject: (NSString *)subject
364
374
withController: (UIViewController *)controller
365
375
atSource: (CGRect )origin
366
- toResult: (FlutterResult)result {
376
+ toResult: (FlutterResult)result
377
+ withResult: (BOOL )withResult {
367
378
NSObject *data = [[NSURL alloc ] initWithString: shareText];
368
379
if (data == nil ) {
369
380
data = [[SharePlusData alloc ] initWithSubject: subject text: shareText];
@@ -372,7 +383,8 @@ + (void)shareText:(NSString *)shareText
372
383
withSubject: subject
373
384
withController: controller
374
385
atSource: origin
375
- toResult: result];
386
+ toResult: result
387
+ withResult: withResult];
376
388
}
377
389
378
390
+ (void )shareFiles: (NSArray *)paths
@@ -381,7 +393,8 @@ + (void)shareFiles:(NSArray *)paths
381
393
withText: (NSString *)text
382
394
withController: (UIViewController *)controller
383
395
atSource: (CGRect )origin
384
- toResult: (FlutterResult)result {
396
+ toResult: (FlutterResult)result
397
+ withResult: (BOOL )withResult {
385
398
NSMutableArray *items = [[NSMutableArray alloc ] init ];
386
399
387
400
for (int i = 0 ; i < [paths count ]; i++) {
@@ -403,7 +416,8 @@ + (void)shareFiles:(NSArray *)paths
403
416
withSubject: subject
404
417
withController: controller
405
418
atSource: origin
406
- toResult: result];
419
+ toResult: result
420
+ withResult: withResult];
407
421
}
408
422
409
423
@end
0 commit comments