Using Swift library in an Objective-C project - ios

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)

Related

Objective c and Swift using in Framework in Xcode

I've my own old framework which is only compatible in objective c, but Now I open in new xcode and added few swift classes (Xcode not gave me option to create bridging as this do in normal App when you add swift class in objective c project), Also Xcode gave me suggestion to update build setting and I updated those and currently I'm on Xcode 8.2.1. Now I try to build but I am getting few error as you can see in picture . I googled and found solution to delete derive data and quite xcode and open and clean. I done Everything but didn't workout.
Is bridging can be possible in framwork.
If not possible then how can I achieve.
Need help and suggestion.
When you are mixing Swift/Objc inside a framework, you can import Swift code for Objective-C use and import Objective-C from Swift.
First, ensure that Define Settings is set for the current framework target.
To import Swift into Objective-C:
In each Objective-C file that requires the Swift code, import the generated Swift header with the complete current Framework Path : #import <Framework/Framework-Swift.h>
To import Objective-C into Swift:
Ensure you have an umbrella header. The path can be found inside the Build Settings of the target. In this header, import all the needed Objective-C headers like normally in Objective-C: #import "Header"
Source: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122

How to use Cocoapods in a Swift framework?

I am trying to create a new Cocoa Touch Framework for iOS in Swift, using some libraries from Cocoapods, but I can't have it work.
I know there are some opened questions about that, but none of them seem to fix my problem.
For testing purposes, I just created an empty Cocoa Touch Framework, installed Cocoapods and added the 'MBProgressHUD' pod.
Then, as documented by Apple ('Importing Code from Within the Same Framework Target' section), I imported the MBProgressHUD header in my umbrella header like that:
#import "MBProgressHUD.h"
But when I compile, I have this error:
include of non-modular header inside framework module
I did set the 'Allow Non-modular includes in Framework Modules' setting to Yes, but it doesn't have any effect.
Is there any way to use CocoaPods in a Swift Framework?
I found the solution, so I will let it here in case someone else has the same issue.
So, Cocoapods supports iOS frameworks since version 0.36, and as it's said in their blog, to use it in a framework, you just have to add this line in your Podfile:
use_frameworks!
After that, in a Swift framework, you don't need to include the .h files in your umbrella header, but you do need to import the module wherever you need it, for instance:
import MBProgressHUD
This is working for me, hope it helps!
Using 'Objective-C bridging header' we can make use of Objective-C frameworks in Swift.
Refer the following, It will explain how to make use of Objective-C framework(For example Parse framework) in Swift.
• Link1
• Link2

Using Obj-C that relies on other Obj-C in Swift?

First I want to note, I've read this and have used it before and my question is different:
How to call Objective-C code from Swift
I'm trying to use this library in Swift.
This library uses OCMock and when I try to compile, I get 'OCMock/OCMock.h' file not found at the line #import <OCMock/OCMock.h>. Here's how I normally fix it, but this time, it's not working.
Just like every other Obj-C library in Swift, I added it to my bridge header, which I already had made and am using for others:
#import "JSQMessages.h"
#import "JSQMessageData.h"
I then added $(PROJECT_DIR) to my Header Search Paths.
Here is the file structure.
From the project directory, it should look for OCMock/OCMock.h, and it is there, as seen in screenshot. So why isn't it working?
I just created a fresh swift iOS project, add JSQMessage using CocoaPod(http://cocoapods.org). No error. try following:
1. Create a new project
2. Create a podfile and add
pod 'JSQMessagesViewController'
3. Create a bridge header and add
#import <JSQMessagesViewController/JSQMessages.h> // import all the things
3. Run
pod install
4. Open project.xcworkspace and build it

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