adding shared extension for ios

I want to add the ability to share to my application on ios. The basic action of adding a shared extension target to the app (at ios folder) causes a cyclic link error. even if I am not writing a single line of code. Anyone was successful at this task?

Yes, I am using the share_plus package. No iOS- or Android-specific code changes necessary!

I am sharing from a ListTile:

// near top of build function
final _shareListTileKey = GlobalKey();

...

// within Widget we are returning in build
ListTile(
  key: _shareListTileKey,
  title: Text(context.loc.shareTheApp, style: linkTheme),
  onTap: () {
    _shareURL(_shareListTileKey);
  }
)

Here is my “share URL” function:

Future<void> _shareURL(GlobalKey shareListTileKey) async {
  String? uriString;
  switch (Theme.of(context).platform) {
    case TargetPlatform.android:
      uriString = await AppConfig().appUrlAndroid;
    case TargetPlatform.iOS:
      uriString = await AppConfig().appUrlIos;
    default: break;
  }

  if (uriString != null) {
    final uri = Uri.parse(uriString);
    final position = RectExt.fromGlobalKey(shareListTileKey);
    Share.shareUri(uri, sharePositionOrigin: position);
  }
}

The sharePositionOrigin is necessary on iPad so the share popover can be located close to the widget you tapped. (iPad crashes if you don’t supply a position.)

Here is the RectExt code:

import 'package:flutter/material.dart';

extension RectExt on Rect {

  static Rect? fromGlobalKey(GlobalKey key) {
    Rect? rect;

    final keyContext = key.currentContext;
    if (keyContext != null) {
      final box = keyContext.findRenderObject() as RenderBox;
      final topLeft = box.localToGlobal(Offset.zero);

      rect =  Rect.fromLTWH(topLeft.dx, topLeft.dy, box.size.width/2, box.size.height);
    }

    return rect;  
  }

}

Hope that helps!

1 Like