Import an Objective-c structure into a Swift environment (Google Analytics + Cocoapod)

I am trying to centralize my frequently used Swift code into the framework, and part of this code uses Google Analytics. I cited Google Analytics as Cocoapod, but I can’t access it from the new structure, as I did from the original project, because it is Objective-C and there is no support for bridge headers in frameworks [I use Swift 1.2].

The line of code that I usually have in the header of the bridge that does all this work:

#import <Google / Analytics.h>

Where exactly do I put this in my project to do it all the way it was done earlier in the header of the bridge?

What I found in the Apple Documentation about mixing Swift and Objective-C is this:

Import code from within the same framework Purpose

If you are writing a structure in a mixed language, you may need to access your Objective-C code from Swift and your Swift code from Objective-C.

Import Objective-C into Swift

To import a set of Objective-C files into your same Swift code, you will need to import these files into the Objective-C header header for the frame.

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

In the "Assembly Settings" section of the "Packaging" section, make sure the "Define" module for this target frame is set to Yes. In your umbrella header file, import each Objective-C header that you want to Swift. For example: Objective-C

import <XYZ / XYZCustomCell.h>

import <XYZ / XYZCustomView.h>

import <XYZ / XYZCustomViewController.h>

The phrase that I find most relevant is:

you need to import these files into the Objective-C probe header for the frame

But what is this file and how to create it?

Apple's documentation is mentioned earlier (in the table):

Objective-C code

Import to Swift

#import "Header.h"

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

So my question is how to import this file (#import <Google / Analytics.h>) into my Swift project so that it can see the structure of the coco-hood of Google Analytics, as I would normally do in a bridge header a regular project?

Update:

I came to the conclusion that perhaps the title of the Objective-C bridge is an .h file with the same name as the project. I now tried adding the import statement there, and I get a message:

! Include a non-modular header inside the 'JBS' frame module

+5
source share
2 answers

The binding header file is a special header file for using the Objective-C file in Swift.

You can find more about the bridge in the Apple doc :

Xcode creates a header file along with the file you created and names it with the name of your product module, and then adds "-Bridging-Header.h". (You will learn more about the name of the product module later in the "Naming Your Product Module" section.)

So, the only thing you need to do is create this file manually by selecting

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

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

ex: ABC-Bridging-Header.h

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

Hope this helps someone!

0
source

The solution is not as simple as for the application. We need to create a module map.

Check out this repo example .

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 for which we need Swift code to access.

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

As convenient as the bridge title, it has one key limitation - you cannot use it inside a framework project. An alternative is to use a module.

To do this, create a directory in the project directory, named for the library you want to use. I did this in a shell outside of Xcodes auspices, calling it CommonCrypto. Inside the directory, create a module.map file that encapsulates the 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 in the Swift Compiler - Search Paths in your project settings. Use $ {SRCROOT} in the module path (for example, $ {SRCROOT} / CommonCrypto) to ensure that the project works regardless of where it was checked.

This makes it easy to import CommonCrypto into your Swift files. Note that consumers of any frameworks you create using this method will also have to add a module to their Swift search paths.

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

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

I removed lexon [system] for security (since it removes the warning) and puts the framework in the same folder as the module.map file.

Also, be sure to include libc++.tbd and other necessary dependencies in your target environment (in the Linked Frameworks and Libraries section of the General tab) if you need them.

0
source

All Articles