4

I created an objective-c pod with two files:

Source/SomeViewController.h
Source/SomeViewController.m

I also created a bridging header in the pod:

Source/Bridging-Header.h

with the content:

#import "SomeViewController.h"

My podspec looks like this:

Pod::Spec.new do |s|
  s.name = 'TestLib'
  s.version = '0.0.1'
  s.license = 'MIT'
  s.ios.deployment_target = '7.0' 
  s.source_files = 'Source/*.{h,m}'
  s.requires_arc = true
  s.xcconfig = { 'SWIFT_OBJC_BRIDGING_HEADER' => 'Source/Bridging-Header.h' } 
end 

I created a demo project did pod init and inserted my pod. Then after pod install I get the following output:

Installing TestLib 0.0.1 (was 0.0.1) Generating Pods project Integrating client project

[!] The `TestLibProject [Debug]` target overrides the `SWIFT_OBJC_BRIDGING_HEADER` build setting defined in `Pods/Target Support Files/Pods-TestLibProject/Pods-TestLibProject.debug.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

[!] The `TestLibProject [Release]` target overrides the `SWIFT_OBJC_BRIDGING_HEADER` build setting defined in `Pods/Target Support Files/Pods-TestLibProject/Pods-TestLibProject.release.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

When I open my TestLibProject.xcworkspace file I see that the pod was installed correctly but the bridging header from the pod is not installed correctly. I my Swift project I tried to do:

let vc: SomeViewController

This gives an error because the bridging header from the pod ist not installed.

How do I have to configure the podspec in order to get the bridging header of the pod being installed correctly?

Christophe
  • 68,716
  • 7
  • 72
  • 138

1 Answers1

1

Podspecs build frameworks, and frameworks cannot include bridging headers. If you want to import non-modular code into a Swift framework, you'll need to use a custom module map, instead, e.g. at MyLib/module.modulemap:

framework module MyLib {
    umbrella header "MyLib.h"

    // Load an SDK header, e.g. CommonCrypto.h
    header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"

    export *
    module * { export * }
}

Once there, you can specify the custom module map both in your Xcode project (either as the MODULEMAP_FILE setting in an .xcconfig file, or as the Module Map File of your target's Build Settings.

Now, the final piece of the puzzle: the podspec. You need to set the module_map:

Pod::Spec.new do |s|
  # …
  s.module_map = 'MyLib/module.modulemap'
end

The above is how SQLite.swift distributes itself, both as a general framework and as a pod.


Edit: Seems like I missed the point of the original question, as was clarified in this thread. The OP wanted to use a pod framework's bridging header to automatically load itself into the installing project's Swift code. This isn't possible. Even if Swift frameworks did support bridging headers, they would only be able to load Objective-C/C code (i.e. private framework code) into the framework's Swift code.

stephencelis
  • 4,954
  • 2
  • 29
  • 22
  • Hi, I tried to add a module map file to my pod spec by setting the s.module_map settings, but when I try to pod lint, I won't resolve the module (located in a .h file) It's funny because it's working great within Xcode, do you have a clue ? – Loegic Aug 31 '15 at 08:33