在flutter应用程序中支持自定义文件扩展名(在flutter中打开扩展名为.abc的文件)

问题描述 投票:0回答:2

因此,我正在导出在我的应用程序中创建的扩展名为 .abc 的文件

现在,如果用户在 Whatsapp 或 telegram 或任何其他平台上共享文件,并且当其他用户尝试打开该文件时,“打开方式”对话框应该出现,列表中应包含我的应用程序。

我尝试过以下方法,但不起作用,而且它仅适用于 Android,我正在寻找多平台解决方案。

是否可以将 Flutter 应用注册为 Android Intent Filter 并处理传入 Intents?

我能够使用上述方法编译应用程序,但无法使用我的应用程序名称填充“打开方式”对话框

感谢您的宝贵时间。

编辑1:

所以我已经成功为 Android 添加了此功能,但我仍在尝试为我的 iPhone 用户添加相同的功能。

有人可以帮我解决 iOS 中的 swift 代码部分吗? 我不熟悉 swift 语言,但我希望我的用户能够在两个平台上使用相同的功能。

flutter flutter-layout flutter-dependencies
2个回答
0
投票

在 iOS Flutter 中,将文件扩展名与应用程序关联涉及修改 Flutter 代码和本机 iOS 平台代码。具体方法如下:

第一步:修改Info.plist 第一步是修改 Info.plist 文件,该文件位于 Flutter 项目的 ios/Runner/Info.plist 目录中。您需要为 CFBundleDocumentTypes 键添加一个条目,它告诉 iOS 您的应用程序可以打开特定的文件类型。

以下示例展示了如何为自定义文件扩展名(例如 .myextension)进行配置:

<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>My Custom File</string>
        <key>CFBundleTypeRole</key>
        <string>Editor</string>
        <key>LSHandlerRank</key>
        <string>Owner</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.mycompany.myextension</string>
        </array>
        <key>CFBundleTypeIconFiles</key>
        <array>
            <string>MyIcon</string>
        </array>
    </dict>
</array>
<key>UTExportedTypeDeclarations</key>
<array>
    <dict>
        <key>UTTypeIdentifier</key>
        <string>com.mycompany.myextension</string>
        <key>UTTypeDescription</key>
        <string>My Custom File</string>
        <key>UTTypeTagSpecification</key>
        <dict>
            <key>public.filename-extension</key>
            <array>
                <string>myextension</string>
            </array>
            <key>public.mime-type</key>
            <string>application/myextension</string>
        </dict>
    </dict>
</array>

说明: CFBundleDocumentTypes:定义您的应用程序支持的文档类型。 CFBundleTypeName:您的文档类型的名称。 CFBundleTypeRole:您的应用程序的角色(例如编辑器、查看器)。 LSHandlerRank:指定您的应用程序是该文件类型的所有者还是默认处理程序。 LSItemContentTypes:列出应用程序支持的统一类型标识符 (UTI)。 CFBundleTypeIconFiles:用于文档类型的图标文件数组。 UTExportedTypeDeclarations:定义由您的应用程序导出的自定义 UTI。

UTTypeIdentifier:UTI 字符串(例如 com.mycompany.myextension)。 UTTypeTagSpecification:将文件扩展名和 MIME 类型映射到 UTI。 第2步:处理Flutter中打开的文件 要在从其他应用程序或直接从 iOS 文件系统打开文件时处理该文件,您需要在 AppDelegate.swift(如果使用 Swift)或 AppDelegate.m(如果使用 Objective-C)中添加特定于平台的代码。

对于斯威夫特:

打开

ios/Runner/AppDelegate.swift
。 重写 application(_:open:options:) 方法来处理文件 URL。

import UIKit
import Flutter

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    override func application(
        _ application: UIApplication,
        open url: URL,
        options: [UIApplication.OpenURLOptionsKey : Any] = [:]
    ) -> Bool {
        if url.isFileURL {
            // Send the file URL to Flutter
            let controller = window?.rootViewController as! FlutterViewController
            let channel = FlutterMethodChannel(name: "com.mycompany.filehandler", binaryMessenger: controller.binaryMessenger)
            channel.invokeMethod("openFile", arguments: url.path)
        }
        return true
    }
}

第3步:处理Flutter中的方法调用 最后,在 Flutter 代码中处理来自 iOS 的方法调用。

import 'package:flutter/services.dart';

void main() {
  runApp(MyApp());

  const platform = MethodChannel('com.mycompany.filehandler');
  platform.setMethodCallHandler((call) async {
    if (call.method == "openFile") {
      final String filePath = call.arguments;
      // Now you have the file path in Flutter, and you can handle it
      print("File path received: $filePath");
      // Add your file handling code here
    }
  });
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('File Handling Example'),
        ),
        body: Center(
          child: Text('Handle file opening'),
        ),
      ),
    );
  }
}

总结: 修改 Info.plist 以注册您的文件扩展名。 重写 iOS 本机代码中的 application(_:open:options:) 来处理文件打开。 在Flutter中处理方法调用来根据需要处理文件。 通过此设置,您的 Flutter 应用程序应该能够在 iOS 中选择具有关联扩展名的文件时打开它们。


-3
投票

我找到了 iOS 的解决方案:

您可以使用 Uni Links 包将通知发送到您的 Flutter 应用程序中。 然后,您需要在 XCode 项目的 Info.plist 中手动添加文件类型。您可以在here找到解释。

© www.soinside.com 2019 - 2024. All rights reserved.