Mixing Objective-C files in Swift Pod - ios

We are creating our private Pod in Swift and have to include some legacy code which is written in Objective-C. I was wondering what's the way to include that in our Pod and use it in swift files?

If you're using the latest cocoapods it should just work. Include the Swift files and the Objective C files in the source_files for the spec and it will all compile together. You can't use a bridging header in a pod, but the Swift classes can access anything that's public in the umbrella header.

Related

How to use FCUUID in a Swift project? [duplicate]

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

Using Swift library in an Objective-C project

I am working in an Objective-C, iOS project, I used danielgindi Charts library which is a Swift library. I downloaded it using Cocoapods.
I am trying to import the library files into my Objective-C files using 'projectName-Swift.h' as mentioned in this question but I faced an error:
'projectName-Swift.h' file not found
If you have installed Swift library using cocoapods
To import Library in Objective C Code
Use
in Objective C
#import frameworkname;
In Swift
#import frameworkname
If your using cocoa pods try to make sure you have use_frameworks! in your cocoa pods pod file, otherwise the Swift Framework wont work. Also look at this for any other issues that may pop up: link
For making the bridging header visible to your compiler you need a little setup
Go to your Project Build Settings
Search for bridging header
Add the path to your
.h file (usually ProjectName/ProjectName-Swift.h)

Swift framework and Cocoapods - integrate static libraries

I created a cocoa touch framework with Swift as the preferred language.
On this framework I need to import static libraries, so I created a podfile and imported the pod using use_frameworks!
I created a bridging header and also added the pods source as header search path since it couldn't find the pod's header.
The framework compiles fine and I have no issues.
So I created a pod spec for the framework and created a sample app in swift. I had the framework as development pod in my podfile. Now I get
Include of non-modular header inside framework module in my bridging header import of the static library public header.
I understand since it's a static library we cannot build the touch framework, but what would be the work around to have a touch framework with static libraries in them written in Swift.
PS I have set Allow Non-modular Includes in Framework Modules to Yes
To answer my original question. It seems to be an issue with cocoapods(0.36.2) for vendor libraries where there is no implementation file.
There is a hack to get this working and you can take a look at this github issue

How to add a swift file to Objective c pod?

I am working on my own private pod and wants to add a swift file to my existing Objective-C private pod.
When I tried to add .swift file Xcode created the Pods-Tests-Bridging-Header.h file.
In-fact I mentioned this file in build settings "Objective-C Bridging Header" also.
As I have read on apple developer docs Its says I have to import a "modfule_name-swift.h" in my files to access swift classes files.
And as separate Xcode project of Objective-C I am able to do that thing by importing "Project_name-swift.h" file in Objective-C Code.
While I am trying to same thing in Pods like "#import "Pods-Tests-swift.h" compiler couldn't recognize it and starts giving errors.
How to do that?
CocoaPods does not yet support Swift

what to do for 3rd party code which was placed in bridging header before integrating pod file

![enter image description here][1]Before using cocoa pods in swift and xCode 6 beta 6 bridging file was used for integrating 3rd party code of obj c. But after making pod file and integrating that 3rd party code library in pod file then what to do for bridging header file and will the pod file library will be available to my code??
It's really not clear what you are asking to be honest. But I will try to answer your question.
The bridging file is for including Objective-C classes that you want to use in your Swift code. It doesn't matter if it's 3rd party code, a Cocoapod, or if you made it yourself.
If you want to access Cocoapod code in your Swift files then just add the Cocoapod as usual (Update Podfile then pod install). Then #import the header file of the pod class that you want to use in your Swift code.
You should then be able to use the pod in your Swift files using Swift syntax.
For example your bridging header would look like:
// My-Bridging-Header.h
#import "AwesomeSauceClassFromAPod.h"

Resources