Search code examples
objective-cswiftfluttercocoapodsflutter-plugin

How to use Objective-C framework in Swift without Bridging Header?


I'm developing a Flutter plugin (in Swift) trying to wrap an Obj-C framework. I was able to import the Header files in MyPlugin.h file, but now how do I use the framework without a Bridging Header? I'm just getting not found in scope.

If I were to generate a Bridging Header, I ran into another error using bridging headers with framework targets is unsupported

This is my podspec file, I had to set DEFINES_MODULE to NO in order to build the project without running into Include of non-modular header inside framework module error

Pod::Spec.new do |s|
  s.name             = 'lisnr'
  s.version          = '1.0.0'
  s.summary          = 'Flutter plugin for LISNR'
  s.description      = <<-DESC
  Flutter plugin for LISNR.
                       DESC
  s.homepage         = 'redacted'
  s.license          = { :file => '../LICENSE' }
  s.author           = { 'redacted' => 'redacted' }
  s.source           = { :path => '.' }
  s.source_files = 'Classes/**/*'
  s.dependency 'Flutter'
  s.platform = :ios, '8.0'

  s.preserve_paths = 'radius.framework'
  s.xcconfig = { 'OTHER_LDFLAGS' => '-framework radius' }
  s.vendored_frameworks = 'radius.framework'

  # Flutter.framework does not contain a i386 slice.
  s.pod_target_xcconfig = { 'DEFINES_MODULE' => 'NO', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386' }
  s.swift_version = '5.0'
  s.public_header_files = 'Classes/**/*.h'
end

The other codes are pretty much generated by the Flutter CLI.


Solution

  • You need a module map. You can either write it manually and put in the framework or do it with a script. See how I did it here for the Spotify SDK which is an ObjC framework.

    The script for you would be something like that:

    #!/bin/sh
    
    MODULE_DIR="MY_PATH/radius.framework" # You framework's path
    mkdir -p "${MODULE_DIR}"
    printf "module radius {\n\
    header \"Headers/radius.h\"\n\
    export *\n\
    }" > "${MODULE_DIR}/module.map"