16

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. 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! 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. 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. 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! from the project 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.

  • any luck with this? im in the same boat... – Derek Hannah Apr 24 '19 at 19:48
  • Well, not really. In the end I discovered that is possible to import different pod from the podcast file, the problem is when there is something that is not possible to get with the command ‘pod ’. At this point is really hard write the right podcast configuration, it depends from how the .framework file is served. Ex: Have a local .framework file that doesn’t have headers to use inside it but outside. – Lorenzo Imperatrice Apr 27 '19 at 21:38
  • @LorenzoImperatrice can you share how you did it the ‘pod ’ way? – Daniel Eberl Mar 12 '20 at 15:58
  • I'm sorry, at the end I didn't do it with 'pod ' because wasn't the right thing at that moment. Anyway do that is pretty easy and there are tons of guides online.. I suggest to always check [link](https://guides.cocoapods.org/syntax/podspec.html) that is the official guide. There is specified that you only need to write something like: `spec.dependency 'SocketRocket'` in your podspec for get the dependency from your project. – Lorenzo Imperatrice Mar 19 '20 at 10:32
  • https://stackoverflow.com/questions/24002369/how-do-i-call-objective-c-code-from-swift try this :) – hio Nov 06 '20 at 10:11
  • @ParthPitroda the problem there was that flutter create everything automatically and in a scalable solution your suggestion is not applicable. Thanks anyway to share it with us. – Lorenzo Imperatrice Nov 06 '20 at 20:08

2 Answers2

1

I suggest you check out starflut package https://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

  • Hey Samuel! Thanks for your suggestion, I'll surely have a look at the package but I don't think that it can help, I've 2 questions open on the same argument, and both of those questions has 10+ votes but no answer :( . Unfortunately, the thing that I had to do, was referred to a paid library that just offers native code and nothing more, my hope was to create a plugin from it but I just ended to bind it to the app code! – Lorenzo Imperatrice Feb 19 '21 at 11:35
1

If it's still a relevant issue for some of developers, here's the possible solution.

In the .podspec file of your plugin paste this string for remote frameworks

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
Den
  • 1,456
  • 16
  • 17
  • Hi mate thanks for your answer, if you start to receive some upvotes please, ping me in the comments, in this way I'll assign to you the best answer. Unfortunately I'm not able to verify it anymore because I don't code in Flutter from long time now. – Lorenzo Imperatrice Dec 14 '21 at 08:29
  • Hi Den, where do you put your WebRTC.framework folder ? Same folder as your podpsec file ? – Arton Hoxha Nov 08 '22 at 17:52
  • @ArtonHoxha Hi, Arton! Yes, keep it in the same folder. – Den Nov 10 '22 at 04:14