Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to use both Swift and Obj-C libs with Cocoapods

I have just started a new Swift project and I would like to use different libraries. In particular, I would like to use Realm.io, an Obj-C library. But, I would also like to use pure Swift libraries such as Alamofire or Dollar.

I use Cocoapods for managing my dependencies. I use the latest version (0.37.0) and the new use_frameworks! flag. pod install is successful anytime.

Unfortunately, when I try to build my project I get two errors (for my main target):

  • Umbrella header Realm.h not found from module.modulemap
  • Could not build Objective-C module Realm from any file using import Realm

Other imports work fine.

I have noticed the following: if I remove pure Swift libs and use_frameworks, everything works fine. I am aware about this current issue from Cocoapods. However, it should not be a problem for Realm asks developers to use that flag.

Here is my Podfile:

platform :ios, '8.0'
use_frameworks!

target 'rothrock' do
  pod 'Realm'
  pod 'Cent'
  pod 'SwiftyJSON'
  pod 'Alamofire'
end

target 'rothrockTests', :exclusive => true do
end

I use no bridging header. Should I?

Any idea or workaround?

like image 462
Adrien Cadet Avatar asked Apr 27 '15 20:04

Adrien Cadet


People also ask

How do I use Objective-C pod in Swift?

Support Objective-C pods in a Swift project First, create your Podfile and add the pods you need as usual. Install them using the pod install command and open the . xcworkspace file created in your project folder. Pods should now be included in your workspace.

Can you use CocoaPods and Swift package manager?

Cocoapods and SwiftPM are some of the most popular package managers. By supporting both in your open-source library, you potentially increase the developers who will use it. 🚨Remember the most important thing all package managers want to know is where are the source files in order to distribute them.

How do I add dependencies to CocoaPods?

After you have initially installed CocoaPods into your project, you can add new dependencies (or remove unused ones) by editing the Podfile. Then simply run pod install again.


1 Answers

Alright, here is the full walkthrough:

  1. Install dependencies using Cocoapods and the use_frameworks! flag.
  2. As you need to use a Objective-C dependency, create a Bridging header. You can create one easily by importing an Objective-C class to your Swift project, than remove it (the wizard should ask you if need a bridging header). Otherwise, create a new header file. Then, navigate to your target configuration and enter the name of your file in Swift Compiler - Code Generation > Objective-C Bridging header.
  3. Still in your target configuration, add a new entry in Search Paths > User Header Search Paths: Pods as value and flag it as recursive.
  4. Remove any import instruction from your code, relative to your Objective-C library.
  5. Build your project. You should have a success.
like image 186
Adrien Cadet Avatar answered Oct 05 '22 10:10

Adrien Cadet