@@ -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,14 +373,16 @@ + (void)shareText:(NSString *)shareText
363
373
subject: (NSString *)subject
364
374
withController: (UIViewController *)controller
365
375
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
368
379
text: shareText];
369
380
[self share: @[ data ]
370
381
withSubject: subject
371
382
withController: controller
372
383
atSource: origin
373
- toResult: result];
384
+ toResult: result
385
+ withResult: withResult];
374
386
}
375
387
376
388
+ (void )shareFiles: (NSArray *)paths
@@ -379,7 +391,8 @@ + (void)shareFiles:(NSArray *)paths
379
391
withText: (NSString *)text
380
392
withController: (UIViewController *)controller
381
393
atSource: (CGRect )origin
382
- toResult: (FlutterResult)result {
394
+ toResult: (FlutterResult)result
395
+ withResult: (BOOL )withResult {
383
396
NSMutableArray *items = [[NSMutableArray alloc ] init ];
384
397
385
398
for (int i = 0 ; i < [paths count ]; i++) {
@@ -398,7 +411,8 @@ + (void)shareFiles:(NSArray *)paths
398
411
withSubject: subject
399
412
withController: controller
400
413
atSource: origin
401
- toResult: result];
414
+ toResult: result
415
+ withResult: withResult];
402
416
}
403
417
404
418
@end
0 commit comments