简体   繁体   English

将Pod依赖项添加到Flutter插件类

[英]Add Pod dependency to a Flutter Plugin Class

Finally I got a custom Flutter Plugin's MethodChannel to do the hello-world. 最终,我得到了一个自定义的Flutter插件的MethodChannel来进行问候。

But now, I would like to add a dependency explicitly to the iOS part of the Plugin Class (obviously not to the Android-part...). 但是现在,我想将一个依赖项显式添加到Plugin Class的iOS部分(显然不添加至Android部分...)。

However, this somehow bites the snake in its tale since the Plugin Class is itself part of the Flutter Pod. 但是,由于插件类本身是Flutter Pod的一部分,因此在某种程度上使这条蛇很伤人。 So how does this work ? 那么这是如何工作的呢?

I added the following inside the /iOS/Podfile 我在/ iOS / Podfile中添加了以下内容

target 'Runner' do
  use_frameworks!

  pod `MyPodDependency`

The code compiles. 代码会编译。

But the import MyPodDependency fails. 但是import MyPodDependency失败。 (inside my Plugin Class where I also program the public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {...} code, to be found under ../Pod/../../../../ios/Classes/MyPluginClass.swift) (在我的Plugin类中,我也在其中编程了public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {...}代码,位于../Pod/../../../ ../ios/Classes/MyPluginClass.swift)

My question: How do I import MyPodDependency inside the iOS part of the Flutter Plugin-Class? 我的问题:如何在Flutter插件类的iOS部分内导入MyPodDependency?

In which of the Podfiles do I need to add my pod. 我需要在哪个Podfiles中添加我的pod。

Can I use any imported Framework inside the Plugin Class (where the FlutterMethodCall's handle method sits)? 我可以在Plugin类(FlutterMethodCall的handle方法所在的位置)内使用任何导入的Framework吗? And if yes, how ??? 如果是的话,怎么办?

You have to remember that a plugin must work in any project, not only in yours. 您必须记住,插件必须在任何项目中都可以使用,而不仅在您的项目中可以使用。 This is why its dependencies shouldn't be inside your project's pod file, but contained in its own dependencies file, otherwise, everyone would always have to add manually each dependency of all the plugins used in a project. 这就是为什么它的依赖项不应该放在项目的pod文件中,而应该包含在它自己的依赖项文件中的原因,否则,每个人总是必须手动添加项目中使用的所有插件的每个依赖项。

That said, your plugin dependencies must be referenced inside the .podspec file, located in the /ios directory of your plugin. 也就是说,您的插件依赖项必须在插件的/ ios目录中的.podspec文件内引用。 This will execute when you run "pod install" in your project, fetching all external dependencies your plugin needs. 当您在项目中运行“ pod install”时,将执行此操作,以获取插件所需的所有外部依赖项。 If you take a look in the existing file, it already presents one dependency: 如果查看现有文件,它已经呈现出一种依赖性:

s.dependency 'Flutter'

Follow the example and add your other plugin dependencies below. 按照示例进行操作,并在下面添加其他插件依赖项。

Finally, you will have to add the plugin to your Flutter project. 最后,您必须将插件添加到Flutter项目中。 Simply add it to the pubspec file, as you would do with all other plugins. 只需将其添加到pubspec文件中即可,就像使用其他所有插件一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM