Flutter iOS许可证信息获取插件dart_license_plist的使用

Flutter iOS许可证信息获取插件dart_license_plist的使用

DartLicensePlist 是一个命令行工具,用于为 Dart 编程语言生成许可证文件。

开始使用

首先,安装 dart_license_plist

$ dart pub global activate dart_license_plist
Resolving dependencies...
...
Downloading dart_license_plist <最新版本>...
Building package executables...
Built dart_license_plist:dart_license_plist.
Activated dart_license_plist <最新版本>.

使用方法

对于iOS平台

在iOS平台上,dart_license_plist 通过项目中的 Settings.bundle 来执行。

如果在 <项目根目录>/ios/Runner 文件夹中不存在 Settings.bundle,则需要创建它。

在Xcode菜单中:

  1. 点击 文件 -> 新建 -> 文件…
  2. 选择 Settings.bundle 并点击 创建

dart_license_plist 中,.lproj 文件夹并不使用,如果没有使用可以删除。

其他平台

处理中…

对于原生使用的开源软件

dart_license_plist 仅支持 Dart 的开源软件。对于原生iOS,需要使用其他工具。

推荐使用 LicensePlist,它还支持 CocoaPods、Carthage 和手动(Git 子模块、直接源码等)。

选项

--custom-license-yaml

如果无法获取包的许可证信息,可以使用 --custom-license-yaml 参数来使用自定义的许可证数据。

解析后的 custom-license-yaml 数据优先于从 pub.devgithub.com 获取的许可证数据。

YAML文件格式

exclude:
  &lt;获取排除的包名&gt;:
  &lt;获取排除的包名&gt;:
  &lt;获取排除的包名&gt;:
  ...

packages:
  &lt;自定义许可证包名&gt;:
    license: |
      &lt;自定义许可证文本&gt;

  &lt;自定义许可证包名&gt;:
    license: |
      &lt;自定义许可证文本&gt;

  &lt;自定义许可证包名&gt;:
    license: |
      &lt;自定义许可证文本&gt;

YAML文件示例

exclude:
  package_name:
  package_name_2:

packages:
  package_name_1:
    license: |
      The MIT License

      Copyright (c) 2022 Hiroki Nomura.
      All rights reserved.

      Permission is hereby granted, free of charge, to any person obtaining a copy
      of this software and associated documentation files (the "Software"), to deal
      in the Software without restriction, including without limitation the rights
      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      copies of the Software, and to permit persons to whom the Software is
      furnished to do so, subject to the following conditions:

      The above copyright notice and this permission notice shall be included in
      all copies or substantial portions of the Software.

      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      THE SOFTWARE.

  package_name_2:
    license: |
      The MIT License

      Copyright (c) 2022 Hiroki Nomura.
      All rights reserved.

      Permission is hereby granted, free of charge, to any person obtaining a copy
      of this software and associated documentation files (the "Software"), to deal
      in the Software without restriction, including without limitation the rights
      to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      copies of the Software, and to permit persons to whom the Software is
      furnished to do so, subject to the following conditions:

      The above copyright notice and this permission notice shall be included in
      all copies or substantial portions of the Software.

      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      THE SOFTWARE.

更多关于Flutter iOS许可证信息获取插件dart_license_plist的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter iOS许可证信息获取插件dart_license_plist的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


dart_license_plist 是一个用于在 Flutter 应用中生成 iOS 许可证信息列表的插件。它可以将项目中的开源库许可证信息打包到一个 .plist 文件中,并在应用中展示这些许可证信息。以下是使用 dart_license_plist 的步骤:


1. 添加依赖

首先,在 pubspec.yaml 文件中添加 dart_license_plist 依赖:

dev_dependencies:
  dart_license_plist: ^2.0.0

运行 flutter pub get 以安装依赖。


2. 配置脚本

在项目根目录下创建一个脚本文件(如 scripts/generate_licenses.dart),用于生成许可证信息:

import 'package:dart_license_plist/dart_license_plist.dart';

void main() async {
  await generateLicensePlist();
}

3. 运行脚本生成 .plist 文件

在终端中运行以下命令来生成 .plist 文件:

flutter pub run scripts/generate_licenses.dart

执行后,插件会将生成的 Licenses.plist 文件保存到 ios/Runner/Resources 目录下。


4. 在 iOS 项目中添加资源

确保生成的 Licenses.plist 文件被正确添加到 iOS 项目的 Runner 中:

  1. 打开 Xcode。
  2. 导航到 Runner -> Resources
  3. 如果 Licenses.plist 文件没有自动添加,手动将其拖入 Resources 文件夹中。

5. 在应用中显示许可证信息

在 Flutter 中,可以通过以下方式读取和显示许可证信息:

import 'package:flutter/services.dart';
import 'dart:convert';

Future<List<Map<String, String>>> loadLicenses() async {
  final String jsonString = await rootBundle.loadString('Licenses.plist');
  final Map<String, dynamic> licensesMap = jsonDecode(jsonString);
  return licensesMap.entries.map((entry) {
    return {'name': entry.key, 'license': entry.value};
  }).toList();
}

然后,在 UI 中显示数据:

class LicenseScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return FutureBuilder<List<Map<String, String>>>(
      future: loadLicenses(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        } else if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        } else {
          final licenses = snapshot.data!;
          return ListView.builder(
            itemCount: licenses.length,
            itemBuilder: (context, index) {
              final license = licenses[index];
              return ListTile(
                title: Text(license['name']!),
                subtitle: Text(license['license']!),
              );
            },
          );
        }
      },
    );
  }
}

6. 配置 iOS 设置(可选)

如果需要自定义生成的 .plist 文件路径或内容,可以在脚本中传递参数:

await generateLicensePlist(
  outputPath: 'ios/Runner/Resources/CustomLicenses.plist',
  includePackages: ['flutter', 'dart_license_plist'],
  excludePackages: ['example_package'],
);
回到顶部