简体   繁体   English

Flutter 插件:在 iOS 和 MacOS 之间共享代码

[英]Flutter Plugin: share code between iOS and MacOS

I have a common dir at the root dir of the plugin that holds code that should be shared by both iOS and MacOS.我在插件的根目录中有一个公共目录,其中包含应该由 iOS 和 MacOS 共享的代码。 I changed the source_files in the .podspec files of both to point to the common dir: s.source_files = '../common/Classes/**/*' , but now when I build either of the ios/macos examples, build fails with error that it can't find the plugin import.我将两者的.podspec文件中的source_files为指向公共目录: s.source_files = '../common/Classes/**/*' ,但是现在当我构建任何一个 ios/macos 示例时,构建失败并显示找不到插件导入的错误。

/Flutter/GeneratedPluginRegistrant.swift:8:8: error: no such module 'plugin_name' /Flutter/GeneratedPluginRegistrant.swift:8:8: 错误:没有这样的模块'plugin_name'

Is this the right way to share code between iOS and Macos?这是在 iOS 和 Macos 之间共享代码的正确方法吗? How do I fix this?我该如何解决?

It appears that it wasn't the right way to share code between iOS and MacOS.看来这不是在 iOS 和 MacOS 之间共享代码的正确方式。 Instead what I should've done is:相反,我应该做的是:

  1. Keep .podspec files unchanged.保持.podspec文件不变。
  2. Create a directory at the root of the plugin codebase called apple (the name doesn't matter).在插件代码库的根目录下创建一个名为 apple 的目录(名称无关紧要)。
  3. Move the common code to the directory created in step 2.将公共代码移动到步骤 2 中创建的目录。
  4. Create symlinks to each common code file from ios/Classes and macos/Classes (assuming in corresponding directory):ios/Classesmacos/Classes创建每个公共代码文件的符号链接(假设在相应的目录中):
ln -s ../../apple/Classes/SwiftSomePlugin.swift SwiftSomePlugin.swift

In my case I had only one file, but if you have more files, you need to create symlink per each file.就我而言,我只有一个文件,但如果您有更多文件,则需要为每个文件创建符号链接。

The end result should look like this:最终结果应如下所示:

├── apple
│   └── Classes
│       └── SwiftSomePlugin.swift
├── ios
│   ├── Assets
│   ├── Classes
│   │   ├── SomePlugin.h
│   │   ├── SomePlugin.m
│   │   └── SwiftSomePlugin.swift -> ../../apple/Classes/SwiftSomePlugin.swift
│   └── some.podspec
├── macos
│   ├── Classes
│   │   └── SwiftSomePlugin.swift -> ../../apple/Classes/SwiftSomePlugin.swift
│   └── some.podspec

Now run the build and it should succeed.现在运行构建,它应该会成功。

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

相关问题 如何在iOS应用程序和macOS应用程序之间共享数据? - How to share data between an iOS app and a macOS app? 在 iOS 和 MacOS 之间共享代码 - NSTextField 和 UITextField - Sharing code between iOS and MacOS - NSTextField and UITextField 使用适用于 macOS/iOS 的 XCFramework 创建 Flutter Rust FFI 插件 - Creating Flutter Rust FFI Plugin with an XCFramework for macOS/iOS 如何在flutter插件中调试iOS本机代码? - How to debug iOS native code in a flutter plugin? 用于在共享扩展和iOS应用之间共享文件路径/文件的代码 - Code to share file path/file between a share extension and iOS app 在iOS客户端代码和Parse云代码之间共享常量 - Share constants between iOS client code and Parse cloud code 在iOS中的多个项目之间共享代码的最佳方式 - Best way to share code between multiple projects in iOS 适用于iOS的NSColorList,或通过某种方式在IB和代码之间共享颜色 - NSColorList for iOS, or some way to share colors between IB and code 如何在多个iOS Swift应用之间模块化/共享代码? - How to modularize/share code between multiple iOS Swift apps? Flutter 插件:iOS 原生代码中没有这样的模块“google_mobile_ads” - Flutter plugin: No such module 'google_mobile_ads' in iOS native code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM