gpt4 book ai didi

android - flutter 在外部打开文件,例如在 ios 上 "open in"

转载 作者:行者123 更新时间:2023-11-30 05:07:48 24 4
gpt4 key购买 nike

据我所知,大多数 Flutter 指南都可以从本地存储打开,但与文件共享无关。任何人都知道如何做到这一点。这是专门为 ios 启用它的指南 https://developer.apple.com/library/archive/qa/qa1587/_index.html .

我的意思是有 https://pub.dartlang.org/packages/open_file扩展名,但从文件存储中打开。

澄清这个问题不是关于与另一个应用程序共享文件,而是当从外部应用程序共享文件时被提示在这个 flutter 应用程序中打开。

最佳答案

要在 iOS 中执行此操作,您首先要按照您提到的指南中的描述在 XCode 中定义文档类型和导入的 UTI,然后在您的 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];
}

在 Dart 方面:

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
});
}
}
}

关于android - flutter 在外部打开文件,例如在 ios 上 "open in",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54245980/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com