简体   繁体   English

如何在 Flutter 插件的 Swift 编写的 iOS 部分中使用 Objective-C 框架

[英]How to use Objective-C framework in a Swift written iOS part of a flutter plugin

In a Flutter plugin , I would like to use an Objective-C framework in my iOS part written in swift , and after that using it in a Flutter project that uses Swift as iOS language.Flutter 插件中,我想在我用 swift 编写的 iOS 部分中使用Objective-C框架,然后在使用 Swift作为 iOS 语言的Flutter 项目中使用它。 After some research about it (I'm not an iOS developer) I find out that what I would like to do is possible by importing the header in the file that act as Bridging header between swift and Objective-C, but the bridging header in the flutter plugin is created automatically if use_frameworks!经过对它的一些研究(我不是 iOS 开发人员)我发现我想要做的事情是通过在文件中导入头文件作为swift 和 Objective-C 之间的桥接头文件来实现的,但是桥接头文件在如果use_frameworks! ,flutter 插件会自动创建use_frameworks! is defined in the Podfile (as far as I know), and I didn't understand how actually import the header from the Objective-C (I think by defining something in the podspec but I don't know) pod in that.是在 Podfile 中定义的(据我所知),我不明白实际上是如何从 Objective-C(我认为是通过在 podspec 中定义一些东西,但我不知道)pod 中导入标头的。 In fact, if I try to import something from the Objective-C pod, XCode complains (and the compiler too) about it by saying that it "doesn't find the module with name <framework name>" or that it can't build Objective-C code.事实上,如果我尝试从 Objective-C pod 中导入一些东西, XCode 会抱怨(以及编译器),说它"doesn't find the module with name <framework name>"或者它不能构建 Objective-C 代码。 If I try to use the framework in an Objective-C plugin I'm able to use it but only if I comment out use_frameworks!如果我尝试在 Objective-C 插件中使用该框架,我可以使用它,但use_frameworks!是我注释掉use_frameworks! from the project Podfile来自项目 Podfile

At the end, I was wondering if it's possible to use Objective-c external framework in Swift plugin for developing a flutter plugin.最后,我想知道是否可以在Swift插件中使用Objective-c外部框架来开发flutter插件。

I suggest you check out starflut package https://pub.dev/packages/starflut .我建议您查看starfluthttps://pub.dev/packages/starflut But most likely what you are trying to accomplish has been done more simply for you in another package but if you are totally bent on doing it yourself I think you should try the package但很可能你想要完成的事情已经在另一个包中更简单地为你完成,但如果你完全一心想要自己做,我认为你应该尝试这个包

If it's still actual issue for some of developers, here's the possible solution.如果对于某些开发人员来说这仍然是实际问题,这里是可能的解决方案。

In the .podspec file of your plugin paste this string for remote frameworks在插件的.podspec文件中,为远程框架粘贴此字符串

Pod::Spec.new do |s|
    ...

    s.dependency 'GoogleWebRTC'
end

Or, for local frameworks, paste these或者,对于本地框架,粘贴这些

Pod::Spec.new do |s|
    ...

    s.preserve_paths = 'WebRTC.framework'
    s.xcconfig = { 'OTHER_LDFLAGS' => '-framework WebRTC', 'ENABLE_BITCODE' => 'NO' }
    s.vendored_frameworks = 'WebRTC.framework'
end

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

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