How to add a swift file to Objective c pod? - ios

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

Related

Could not build Objective-C module, when using swift in objective-c module

In an iOS application I have a subproject (not cocoapods) in which I have included a swift file and ObjC file (that is used by the swift file). XCode automatically created a bridging file but could not build it because apparantly bridging is not allowed in a framework. The workaround that I used was to add the objective-c header to the umbrella file and it worked. Now I need to use a swift class from ObjC. I have define module to set to YES, the generated file Framework-Swift.h . But when I try to import it in objective-c i get
Could not build Objective-C module
The closest I got after some googleing was this answer:
Ah gotcha. It looks like you're building a mixed Swift & Objective-C
pod - if that's the case, Xcode will try to import
within the generated -Swift.h header.
You'll need to create the header manually and add imports for the
Objective-C classes that you want to expose to Swift.
CocoaPods generates an umbrella header automatically and imports it
within the .modulemap, but Xcode doesn't use that when generating the
-Swift.h header
But I am unsure what header needs to be created manually.
Any ideeas or pointer about using swift in an objective-c framework ? In both ways ?
I also had similar issue when using Swift pods in my Swift project containing several targets. No Objective-C code at all. I tried to clear build folder, pods cache, derived data - nothing worked.
Solution:
Open the Build Settings for a target that contains your module code. Set the "Install Objective-C Compatibility Header" to "No"
There's a great and simple article that wraps up this case:
DEFINES_MODULE=YES
To use ObjC classes in Swift, create a bridging header and specify path to it in the build settings
To use Swift classes in ObjC, #import <ModuleName/ModuleName-Swift.h> in your *.m file (use #class SwiftClass; forward declaration in *.h file, if needed)
For swift classes and their members to be visible to objc
inherit your class from NSObject
prepend it with #objcMembers
make both the class and its members public

how can I access my swift project files from pod file which is in objective c?

I am using firebase chat SDK which is in objective c and now I need to access swift files in this SDK. Please help me out.
First create the bridging header for your project by:
Adding the new .h file(header file) to your project and name it as
yourProjectname-Bridging-Header.h
2.Then in the build settings of your project add the path of the bridging header.
$(PROJECT_DIR)/$(PROJECT_NAME)/$(PROJECT_NAME)-Bridging-Header.h
In your bridging header file
# import "frameworkname.h" // your framework's header file
Try to run the code now.
Basically you need to create an header (.h) file that will expose the swift class you chose to expose to objective-c code
There are certain things to consider when doing so and they are all listed in this short article
Add "TargetName-Swift.h" where TargetName is the name of your project, where you want to acess the swift files

How to import Objc Files into my Swift files within my custom cocoapods framework?

Simple explanation. I have created my own pod with name BSSelectableView, but within I need to use two files written in Objective-C. What do I need to achieve?
I need to make it visible within my Swift files, and also for people who will install my pod.
Your Target needs a Bridging Header.
This is an Objective-C-Header-File with the name [TargetName]-Bridging-Header.h
Within this file, you have to #import all Objective-C-Headers you want to use in your Swift-Files.
You don't have to import anything in your Swift files.
More information about Swift and Objective-C mixed projects
You need to create a [TargetName]-Bridging-Header file in your project.
import all the header files in bridging header file
Now you can use all objective c file in swift.

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)

importing PebbleKit in Xcode 6 for use in swift

I'm trying to develop a Pebble companion app in Xcode 6 using swift.
I've followed the instructions so far as could be followed from the pebble development site
Pod seems to install and link PebbleKit without any errors, but when I type the "import PebbleKit" directive in a swift file, it says module not found.
Not being an expert in Objective-C, (nor swift, for that matter) I'm at a loss as to why it isn't working.
Any insights?
For me worked the following solution.
1. I added the PebbleKit manually (all steps for this are described in the link provided by the question)
2. I made a Bridging Header file in the Swift project (check Bridging Headers)
3. In the created Bridging Header file, I wrote: #import
No need of adding 'import PebbleKit' into any Swift file! After I could use all classes from the PebbleKit framework without any problems.

Resources