How to use Objective-C bridging header in a Swift project - ios

I've been asked to use a Objective-C framework in my Swift project.
But I have no idea how to accomplish this.
I've added this file:
Objective-CBridgingHeader.h
Inside this file I've put:
#ifndef Objective_CBridgingHeader_h
#define Objective_CBridgingHeader_h
#import <FMShop/FMShop.h>
#endif /* Objective_CBridgingHeader_h */
Now I was expecting to be able to:
Import FMShop
And get access to that framework using Swift code.
However when I try to
Import FMShop
My project no longer compiles claiming that there:
"No such module 'FMShop'"
What am I missing here?
My base SDK is iOS 8.0 and I'm using Xcode 7.3.1
This is what my project looks like:

Add a new Objective-C file to your Xcode project. Name it as you please and you should get an alert box asking if you would like to create a bridging header. Then delete that file as it was just used to create Bridging Header.
Alternatively you can you can create a bridging header yourself by choosing File > New > File > (iOS, watchOS, tvOS, or macOS) > Source > Header File. Name the file as "ProjectName-Bridging-Header.h" e.g. if Project Name is Test then give file name as "Test-Bridging-Header.h".
https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Go to build settings and search for Objective-C Bridging Header key and provide path of your bridging header file.In my case it is "Main/Main-Bridging-Header.h".

Related

objective-c Bridging header not found in swift compiler-code generation

I am doing an ios app in swift 3. And I want to add ARKitwhich is available in objective-c. I dragged all objective-c files into my project then it asked to create bridging header. So I let it to create. And I imported all objective-c.h files inside that like this.
But when I try to build its showing 22 number of issues like UIViewcannot identify etc..
Then I tried to add the bridging headerpath to build settings under swift compiler - code generation. But I dont see objective-c Bridging headersection under that.
Please help me. What can I do to add these objective-c files into my swift 3 project successfully.
Thanks
The Objective-C bridging header setting can be found under Swift Compiler - General under Build Settings.
But I like to add the bridging header by having Xcode generate it for me. I add a junk Objective-C class by File > New > File... and it will ask me if I want to add a bridging header to my project.
If you do that, you'll only need to move whatever you have in your bridging header now to that one and delete the header you have now. The header will be set properly in the project's settings.
Make sure your build setting is selected as "All" and "Combined" look at below link
Swift Compiler - General is missing in Xcode 9.1

Expose Objective-C Framework to Swift

I'm writing a Cocoa Touch Static Library in Objective-C. It will be distributed through Cocoapods and the developers using it should also be able to do this in Swift.
In my test projects the library can be used perfectly in Objective-C, but in Swift I get the compiler error: No such module MyLibrary.
In the Library Target Defines Module is set to YES. According to the Apple guide here, that's the only thing one must do: https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
To implement pch file in existing swift project:
File > New > File... (macOS app for your case)
then you can import your Objective-C library like this in your pch file (test.pch is my file)
#ifndef test_pch
#define test_pch
#import <YourLib/YourLibMainClass.h>
// Include any system framework and library headers here that should be included in all compilation units.
// You will also need to set the Prefix Header build setting of one or more of your targets to reference this file.
#endif /* test_pch */
Now you need to tell your project there is a pch file.
Go to your target build setting and make a search for "Prefix Header".
At Apple LLVM 8.0 - Language section add value for Prefix header key (be careful with your pch file project path)
You will could import your library on a swift file
import YouLibrary

Bridging Objective-C into Swift Project

I'm trying to put a bridging header file into my Swift project so I can use the Venmo SDK. I've placed the header file where I believe it should go and have added it to the Swift Complier - Code Generation section under build settings.
The bridging header is titled BridgingHeader.h and is directly under the project subsection (would post photo but I don't have enough reputation). It's in the same location as this link:
Under the swift compiler - code generation my path to the bridging header is BridgingHeader.h. Initially I tried to reference the file from the root directory but I still had an error saying error:
bridging header '/Users/j9/Desktop/uSell-swift/BridgingHeader.h' does not exist
Apparently, the compiler is looking for .../BridgingHeader.h, while the actual name is LearnSwift-Bridging-Header.h.
By the way, you can avoid these annoyances by adding an ObjC class File->New->Source->Objective C file... It will create and configure the header for you.

Restkit in Swift Project

I have imported the RestKit to my swift project. But I'am not able to import the Restkit.h file in my swift file. Can anyone please tell me the steps to follow so that I can import the Restkit.h file to my swift file? Is RestKit available in swift language?
Sure you can use.
Create bridging header file inside your project. Name it as [your-project-name]-Bridging-Header.h.
Import RestKit.h header into your bridging header file that you created previous one to integrate with this file. https://github.com/RestKit/RestKit/blob/development/Code/RestKit.h
[your-project-name]-Bridging-Header.h
#import "RestKit.h"
Don't forget to copy RestKit project folder into your project folder.
Reference:
http://blog.revivalx.com/2014/12/30/bridging-between-swift-and-objective-c-in-the-same-project/
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
You will need to add the RestKit framework to your Bridging Header file so that your Swift file can use it. If you don't have a Bridging Header file, you can create one and name it {Your_Product_Module_Name}-Bridging-Header.h. The Product Module Name is in your target's Build Settings. Check this link out for more details on using Swift and Objective-C in the same App Target (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_79)

How to get the Local Host Ip address on iPhone in swift?

I want to get the Local Host Ip address on iPhone in swift ?
I have reference the How to get Ip address in swift , It needs to #include <ifaddrs.h> and the link mention the getifaddrs() is defined in <ifaddrs.h>, which is not included by default. Therefore you have to create a bridging header and add #include <ifaddrs.h>
What is the create the bridging header mean here ?
How to create the bridging header?
Is it mean , copy the ifaddrs.h file to my swift project ?
or does there has other way to get the Local Host Ip address on iPhone in swift?
Thanks in advance.
To import a set of Objective-C files in the same app target as your Swift code, you rely on an Objective-C bridging header to expose those files to Swift.
Xcode offers to create this header file when you add a Swift file to
an existing Objective-C app, or an Objective-C file to an existing
Swift app.
Alternatively, you can create a bridging header yourself by choosing
File > New > File > (iOS or OS X) > Source > Header File.
You’ll need to edit the bridging header file to expose your
Objective-C code to your Swift code
To import Objective-C code into Swift from the same target
In your Objective-C bridging header file, import every Objective-C
header you want to expose to Swift.
Under Build Settings, make sure the Objective-C Bridging Header
build setting under Swift Compiler - Code Generation has a path to
the header.
You can find more info on bridging-header here

Resources