How to use Objective-C CocoaPods in a Swift Project - objective-c

Is there a way I can use a CocoaPod written in Objective-C in my Swift project using swift?
Do I just make a bridging header? And if so, can I access the objects, classes, and fields defined by the libraries in the CocoaPod in Swift?

Basic answer to your question is Yes, you can use objective-c code built with CocoaPods.
More important question is "How to use such libs?"
Answer on this question depends on use_frameworks! flag in your Podfile:
Let's imagine that you want use Objective-C pod with name CoolObjectiveCLib.
If your pod file uses use_frameworks! flag:
// Podfile
use_frameworks!
pod 'CoolObjectiveCLib'
Then you don't need add any bridge header files.
Everything that you need is import framework in Swift source file:
// MyClass.swift
import CoolObjectiveCLib
Now you can use all classes that are presented in lib.
If your pod file doesn't use use_frameworks! flag:
// Podfile
pod 'CoolObjectiveCLib'
Then you need create bridging header file and import there all necessary Objective-C headers:
// MyApp-Bridging-Header
#import "CoolObjectiveCLib.h"
Now you can use all classes that are defined in imported headers.

In podFile use the flag use_frameworks!
Inside Xcode in the Pod folder structure in the dependency, you add xxxxxxx-umbrella.h in Support Files.
In your {PROJECT_NAME}-Bridging-Header.h use:
#import "xxxxxxx/xxxxxxx-umbrella.h"
It works for me.

You just need a bridging header and import there what you need.

AND don't forget to add Bridging Header file name to Target -> Build Settings -> Objective-C Bridging Header

Related

Getting error "Include of non-modular header inside framework module"

I have a private pod written on top of CommonCrypto which explicitly relies on CommonCrypto. Headers have types declared by CommonCrypto like:
#include <CommonCrypto/CommonDigest.h>
typedef CC_SHA256_CTX qwer_digest_evp;
Since CC_SHA256_CTX is declared in CommonCrypto, I can't simply move the header import into implementation file.
I use cocoapods to integrate this pod into my project, and tried the below post install hook, but it didn't work (picked from SO ).
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
target.build_settings(config.name)['CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES'] = 'YES'
end
end
end
An effective option might be to build a module version of CommonCrypto (maybe with added CocoaPods magic). This can be done by following the steps in the answer to Importing CommonCrypto in a Swift framework. The solution functions just as well with Objective-C or C in your Xcode project.
To quote the answer:
I've added some CocoaPods magic to jjrscott's answer in case you need to use CommonCrypto in your CocoaPods library.

Quickblox-WebRTC iOS: QBRTCVideoRenderer property has error for rendererView, unknown type view

I have used Quickblox-WebRTC, but it fails to import in swift files
Here is my pod file looks like:
platform :ios, '9.0'
# Uncomment this line if you're using Swift
use_frameworks!
target 'QuickBloxCall' do
pod 'QuickBlox', '~> 2.6.6'
pod 'Quickblox-WebRTC', '~> 2.2'
end
I have included the QuickbloxWebRTC and Quickblox in my Bridging header.
And I am getting below error while compiling:
Can somebody point out what I did wrong?
Found it. Very silly mistake of mine. Just add #import <UIKit/UIKit.h> as first statement

Objective C Static Library with Pod Dependencies swift file error

I have a static library that is used by two of my projects. This static library is using CocoaLumberjack and RNCryptor. They worked fine before but after I updated the pods they now have a swift file and this causes an error "Swift is not supported for static libraries.". The static library is in Objective-C, one of the projects using it is also in Objective-C, while the other one is in Swift.
I've read other questions and found that most of them say to change the static library to framework. How do I actually do that? Or is there another way to do it?
Thanks.
You may want to uninstall the pod first. Afterwards reinstall the pod, but make sure you include in your Podfile before installing:
use_frameworks!

CocoaPods Failed to Lint A Podspec Due to Objective-C Generated Interface Header Not Found

When I tried to build a pod which some Swift files importing the project's Objective-C generated interface header( something likes YourProductModuleName-Swift.h ), it was always failed to build and said that the Objective-C generated interface header cannot be found.
How can I make the CocoaPod to correctly recognize the Objective-C generated interface header?

Cocoapods podspec dependency import file not found

I'm trying to make a Cocoapod that depends on another but I'm having issues at compile time. Say in this case MyApp is using CocoapodA and CocoapodB, B relies on A.
MyApp Podfile:
platform :ios, '5.0'
pod 'CocoapodA'
pod 'CocoapodB', :path => '../../CocoapodB'
CocoapodB Podspec:
s.dependency 'CocoapodA'
but when I try to compile I get 'CocoapodA/CocoapodA.h' file not found where the import in CocoapodB is trying to include it.
I've tried reading the Podspec documentation but I didn't really get what I'm missing. I also tried s.library = 'CocoapodA'.
Make sure that you correctly specify header files for CocoapodA, for example:
s.public_header_files = 'CocoapodA/**/*.h'
My problem ended up being that the OTHER_LDFLAGS were being overridden. After selecting the Other Linker Flags key (build settings) and pressing backspace it now builds.
Weirdly I can only build on actual device; both simulator and archive are failing. They're probably caused by something else.