флаттер открытый файл внешне такой как на ios "open in" - PullRequest
0 голосов
/ 18 января 2019

Из того, что я могу сказать, большинство руководств по флаттеру могут открываться из локального хранилища, но ничего о совместном использовании файлов. Кто-нибудь знает, как это сделать. Это руководство по включению специально для ios https://developer.apple.com/library/archive/qa/qa1587/_index.html.

Я имею в виду расширение https://pub.dartlang.org/packages/open_file, но открывается из хранилища файлов.

Чтобы прояснить этот вопрос, речь идет не о том, чтобы поделиться файлом из приложения с другим, а о том, чтобы поделиться с внешним приложением, когда ему предлагается открыть это приложение.

1 Ответ

0 голосов
/ 14 июня 2019

Чтобы сделать это в iOS, вы сначала определяете типы документов и импортируемые UTI в XCode, как описано в упомянутом вами руководстве, а затем в файле AppDelegate.m вы делаете:

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    /* custom code begin */
    FlutterViewController* controller = (FlutterViewController*)self.window.rootViewController;
    FlutterMethodChannel* myChannel = [FlutterMethodChannel
                                          methodChannelWithName:@"my/file"
                                          binaryMessenger:controller];
    __block NSURL *initialURL = launchOptions[UIApplicationLaunchOptionsURLKey];

    [myChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
        if ([@"checkintent" isEqualToString:call.method]) {
            if (initialURL) {
                [myChannel invokeMethod:@"loaded" arguments: [initialURL absoluteString]];
                initialURL = nil;
                result(@TRUE);
            }
        }
    }];
    /* custom code end */

    [GeneratedPluginRegistrant registerWithRegistry:self];
    // Override point for customization after application launch.
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

На дротикесторона:

class PlayTextPageState extends State<MyHomePage> with WidgetsBindingObserver{
  static const platform = const MethodChannel('my/file');

  void initState() {
    super.initState();

    WidgetsBinding.instance.addObserver(this);

    platform.setMethodCallHandler((MethodCall call) async {
      String method = call.method;

      if (method == 'loaded') {
        String path = call.arguments; // this is the path
        ...
      }
    });
  }

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);

    if (state == AppLifecycleState.paused) {
      ...
    } else if (state == AppLifecycleState.resumed) {
      platform.invokeMethod("checkintent")
        .then((result) {
          // result == 1 if the app was opened with a file
        });
    }
  }
}
...