How do I create an Objective-C bridging header? - ios

I'm following this guide on adding OneSignal to React Native, and there is one section where is says:
Open NotificationService.m or NotificationService.swift and replace the whole file contents with the code below:
<provides code to add>
If you are using Swift, make sure to create a separate Objective-C Bridging Header for your OneSignalNotificationExtensionService and add the following import:
#import "RCTOneSignalExtensionService.h"
Then you need to tell your Xcode project settings what your bridging header is named, like this.
I added the code they provided to NotificationService.swift because that's the only one of the two files that exists in my project. So I assume I'm "using swift" as they put it. The problem is that when they say to create a separate Objective-C Bridging Header, I don't know how to do that. All I've been able to find online is that when you import Objective-C code into your swift project, Xcode should automatically prompt you to create a bridging header. Xcode hasn't done that for me.
Does anyone know how I can create an Objective-C bridging header?

You have to create a new objective c class:
then
then you name it and specify as objective-c class at language
then you select your directory and hit create. At this point xcode is going to ask you whether or not you want to create bridging header. Yep do it.
And at the end it should look more or less like this at your project.
hope that helps you.

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

CocoaPods 1.0.1 still requires bridging header with Swift

In this example project, comment out the one #import line in BridgingHeader.h and it will yield an error of unresolved identifier in AppDelegate.swift
https://github.com/lacyrhoades/GLSlideshow/tree/stackoverflow
As another approach, and what I understand is the "correct" approach, if in AppDelegate.swift we add import GoogleCastRemoteDisplay this does NOT fix the error, but it makes a new one saying no such module GoogleCastRemoteDisplay
Is this a problem with the library's podspec? I want to make my own Pods but I am confused: When is a bridging header needed for a Pod, when is it not?
A bridging header is required when you use Obj-C based pods. If you use a swift based pod, no bridging header is required.
An easy way to add it is to create a new Obj-C based class in your swift based project (name it whatever. It doesn't matter). When you do that it will ask you if you want to automatically add the BH. Say yes and then delete the class you just created leaving the BH. Import all Obj-C headers here.
Hope that helps.

How to use an Obj-c Library/Custom interface in a Swift Project

Trying to understand how I can use this in my swift project.
https://github.com/Grouper/FlatUIKit
I have copied the classes folder into my project but am not sure how to use the various .h and .m files. How would I go about using these files within my storyboard to use the custom appearances?
Did some searching and wasn't really able to understand the various threads that I found.
A bit of a broad question so I'll try to provide some basics and hopefully that helps.
If you are unaware, there is a whole book written by Apple about how to use objective-c with swift. it's available on iBooks for free Using Swift with Cocoa and Objective-C (Swift 2.1) by Apple Inc
Here is the online link
You will need to create an objective-c bridging header and import your code through the header.
Your bridging header would look like this:
#import "XYZCustomCell.h"
#import "XYZCustomView.h"
#import "XYZCustomViewController.h"
If they don't have any modules then you can use them in your swift code and it should see them. According to Apple:
Use your custom Objective-C code with the same Swift syntax you use with system classes.
let myCell = XYZCustomCell()
myCell.subtitle = "A custom cell"
If you are importing an Objective-c framework then it should already contain and umbrella header file that takes care of the bridging header stuff for you. then you just import the framework name into the class that you are planning on using.
import MyCustomFramework
Link to the apple docs here
For this, you can use a bridging header, which is used to expose Objective C code to swift. To do this, make a new header file in your project. Import the header files you would like to use in the new header file like this:
#import <FlatUIKit/FlatUIKit.h>
And then, in the build settings of your project, define the Objective-C bridging header to be the header file you just created. Now in any swift files that you would like to use the library in just import the classes like this:
import FlatUIKit
Hope this helps!

How to solve Bridiging header error in swift

Trying to call objective c code in swift code but it don't let me to do, it tell header file is missing.
The easiest way to create an Objective-C bridging header and all the project settings to support it is to add any Objective-C file to your project.
Go to File\New\File…, select iOS\Source\Cocoa Touch Class and then click Next. Enter FakeObjectiveCClass as the Class name and choose NSObject as the subclass. Also, make sure the Language is set to Objective-C! Click Next, then Create.
When prompted Would you like to configure an Objective-C bridging header? select Yes.
You’ve successfully created an Objective-C bridging header. You can delete FakeObjectiveCClass.m and FakeObjectiveCClass.h from the project now, since you really just needed the bridging header.
I got an solution for bridging-header.h file does not exit. Just want to share, hope it may help others too.
Follow below steps:-
First of all delete all header file if you have implemented any.
Add new header file from the ios templet. Hope you know how to add any new file, just follow in same manner.
Now go to the buid setting and there search for objective-c bridging header, simply put the header file name with .h extension too.
please see the image how to add the file name in objective-c Bridging Header
In Xcode, add a new class with Objective-C as the language. When prompted to create a bridging header file, click yes.
You will have a file added to your project named: YourProjectName-Bridging-Header.h.
Add all your imports to this file.
In your Swift classes, import the bridging-header file.

Expose Swift Files to Objective-C within Framework

I have a framework with Swift and Objective-C in it. I have the statement at the top of my Objective-C class #import "MyFrameworkHeader.h" which I thought would expose my swift code to my Objective-C class however the compiler still says the symbols don't exist, how can I expose my Swift classes to my Objective-C classes within the same Framework?
Ugh, after smacking my head for a few hours then finally posting this question, within a few minutes I found the answer:
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_82
Under the header: "Importing Swift into Objective-C"
To get Objective-C to register your swift code you should go into your build settings and then see/set your project name, also see if the compatibility header option is checked to yes.
after that is set just go into your file and type #import "-Swift.h". I don't think you can import individual files with swift and Objective-C, you import all of your swift files at once. It's important that you make sure all of your swift files have no errors or you will get an error about this header not being defined. You might have to build your project with no errors for the file to get generated at least once.
I think what you're looking for is a Bridging Header.
This is a file that you import your Objective-C headers into that get exposed to Swift.
There are 2 ways to make one (as far as I know):
The easiest way, in my opinion, is to add an existing Obj-C file to your swift project, or vice-versa. Xcode should ask you if you want to automatically configure a bridging header. Choose yes, and Xcode should make a file called something like 'project name'-bridging-header.h. In this file, import your files, so #import "MyFrameworkHeader.h" should do it.
Make your own empty file with File > New File (Cmd+N) > Source > Header File. Call it whatever, and import your files like in the previous one. Before it will work, you have to enter the name of your bridging header file into a field in your Project Settings (In the first option, Xcode will do it for you).

Resources