12
votes

I'm trying to centralize my commonly used Swift code into a framework, and part of that code uses Google Analytics. I brought in Google Analytics as a Cocoapod, but I can't access it from the new framework like I did from the original project because it's Objective-C and there's no bridging header support in frameworks [I'm using Swift 1.2].

The line of code I normally have in the bridging header that makes all of this work is:

#import <Google/Analytics.h>

Where exactly do I put this in my project to make this all work like it did before in the bridging header?

What I found in Apple's documentation about mixing Swift and Objective-C is this:

Importing Code from Within the Same Framework Target

If you’re writing a mixed-language framework, you may need to access your Objective-C code from Swift and your Swift code from Objective-C.

Importing Objective-C into Swift

To import a set of Objective-C files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header for the framework.

To import Objective-C code into Swift from the same framework

Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. In your umbrella header file, import every Objective-C header you want to expose to Swift. For example: OBJECTIVE-C

import <XYZ/XYZCustomCell.h>

import <XYZ/XYZCustomView.h>

import <XYZ/XYZCustomViewController.h>

The phrase I take to be most relevant is where it says:

you’ll need to import those files into the Objective-C umbrella header for the framework

But what is this file and how to you create it?

Apple's documentation mentions earlier (in a table):

Objective-C code

Import into Swift

#import "Header.h"

Well, I tried just creating a file "Header.h" and importing it, but that doesn't work. I don't know what they're trying to say. I can't find an "umbrella" anything in the build settings.

So my question is, how can I import this file (#import <Google/Analytics.h>) in my Swift project so that it can see the Google Analytics cocoapod framework like I would normally do in the bridging header of a normal project?

Update:

I've come to believe that perhaps the objective-c bridging header is the .h file of the same name as the project. I've now tried adding the import statement there, and the error I get is:

! Include of non-modular header inside framework module 'JBS'

3
I'm struggling with this same thing -- did you ever sort it?.Geoffrey Wiseman
Not yet. I've been avoiding cocoapods because of this problem, but I'm technically using an older implementation of Google Analytics because of this. I'm expecting that Apple will realize the problem at some point and do something about it. That is, unless there's a way already and I just don't know what it is.John Bushnell
@John were you able to resolve the "Include of non-modular header inside framework module" issue by any chance? I'm trying to do the same thing (import GoogleAnalytics cocoapod framework into a swift framework via the umbrella header) and I'm getting the same error.nebs
@nebs I believe so. At least it's progress. See my answer for an explanation.John Bushnell

3 Answers

3
votes

The solution is not as straightforward as for an app. We have to create a modulemap.

Have a look at this example repo.

Inside Swift code we can only import so called modules. The trick is to define a module which in turn contains all the ObjC headers that we need the Swift code to access.

The module map section of this article may also help you.

As convenient as the bridging header is, it has one key limitation—you can’t use it inside a framework project. The alternative is to use a module.

To do this, create a directory in your project directory named after the library you want to use. I did this in the shell, outside Xcode’s auspices, naming it CommonCrypto. Inside the directory, create a module.map file that encapsulates library settings. For CommonCrypto, module.map looks like this:

module CommonCrypto [system] { header "/usr/include/CommonCrypto/CommonCrypto.h" export * }

Now add the new module to Import Paths under Swift Compiler – Search Paths in your project settings. Use ${SRCROOT} in the module path (e.g. ${SRCROOT}/CommonCrypto) to insure that the project works no matter where it’s checked out.

This makes it possible to just import CommonCrypto in your Swift files. Note that consumers of any frameworks you build using this technique are also going to have to add the module to their Swift search paths.

I followed the procedure in the article above and made it work for my need this way:

module Muse { header "Muse.framework/Headers/Muse.h" export * }

I removed the [system] lexon for safety (as it removes warning) and put the framework inside the same folder as the module.map file.

Also, don't forget to include libc++.tbd and other required dependencies in your framework target (in the Linked Frameworks and Libraries section of the General tab) if you need them.

5
votes

For those who have continued to ask if I had solved this problem, the following is how I accomplished this task importing the KochavaTracker SDK Cocoapod into a framework. That SDK is currently written in Objective-C. This answer is based on the answer provided by nuKs, with these specific steps.

1) Create a folder named KochavaTracker under your project folder, i.e. MyFramework/MyFramework/KochavaTracker.

2) Within that folder create a file named module.modulemap and insert the following content:

/*
 This is the private module which is used to make private ObjC headers available to Swift code.
 Note how all header files need to be specified with paths relative to this file.

 This file lives inside a folder, and that folder is the actual module. In Xcode the SWIFT_INCLUDE_PATHS needs to include the parent directory to that folder.
 */
module KochavaTracker {
    header "../../Pods/KochavaTrackeriOS/KochavaTrackeriOS/Classes/KochavaTracker.h"
    export *
}

This effectively creates a module which is a wrapper for interface of the SDK, which Swift can later import like it does other Swift modules. Note that it relies on a relative path to the Pods folder.

3) As found in build settings, modify your SWIFT_INCLUDE_PATHS to include:

$(SRCROOT)/KochavaTracker

This makes it so that you will be able to import the module and it will locate it in/as the KochavaTracker folder which you created under it.

4) Add to your Swift code, where appropriate:

import KochavaTracker

From there you should be able to reference the classes in the KochavaTracker module.

-2
votes

The bridging header file is a specific header file to use Objective-C file into a Swift framework.

You can find more about bridging in the Apple doc :

Xcode creates the header file along with the file you were creating, and names it by your product module name followed by adding "-Bridging-Header.h". (You’ll learn more about the product module name later, in Naming Your Product Module.)

So the only thing you have to make is create that file manually by choosing

File > New > File > (iOS, watchOS, tvOS, or OS X) > Source > Header File.

If the name of your framework is ABC,then the name of the header file should be :

ex : ABC-Bridging-Header.h

You can put it where you want in your framework project.

Hope this can help someone !