CocoaPods 1.0.1 still requires bridging header with Swift - ios

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.

Related

How do I create an Objective-C bridging header?

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.

myProjectName-Swift.h not found after clean build

Spent a lot of time on finding the solution on google and SO but no success. Very hope someone can point out what can be the problem.
So I have objc+swift project. I have a Bridging Header file with imports of Objc header files that I need to use in Swift.
To explain the issue I'll share 2 scenarios. In the 1st scenario, everything works fine. In 2nd it shows an error.
Please note: Both scenarios have the same code base.
Scenario 1. Doesn't work, shows error.
I open the project.
Hard Clean it.
Build
Shows error: 'myProjectName-Swift.h' file not found
It also shows such error:
failed to emit precompiled header
'/Users/tungfam/Library/Developer/Xcode/DerivedData/myProjectName-ctxxkwqtckhvyoawavmuzmdxqaml/Build/Intermediates.noindex/PrecompiledHeaders/myProjectName-Bridging-Header-swift_1UP5PCPCLBPHP-clang_18PVO5108TD8S.pch'
for bridging header
'/Users/tungfam/Developer/myProjectName/myProjectName/App/myProjectName-Bridging-Header.h'
Scenario 2. How to make it work.
I take the same code from Scenario 1.
Hard Clean it
Comment the imports in Bridging Header file
Comment the Swift code where I used Obc files (that were declared in bridging header that I just commented in previous step)
I build the project. It succeeds.
Since some parts in the app are broken cuz I commented stuff. I uncomment the imports in Bridging Header file and the Swift code where I used Objc classes.
Run again (without hard clean) and everything works.
Will really appreciate any help on this issue. Please share anything you think that may help to fix this issue.
I'm using Xcode 10.0; Swift 4.2
UPDATE1: I think it has to do something with the 2nd error I placed above. Maybe it can't generate that bridging file.
UPDATE2: I read something like: "If you are importing the Objc file named ABC into Bridging Header. And if this ABC imports into himself the file myProjectName-Swift.h. Then this case may have some problems. Do you think it can be true?
You must not use #import "ProjectName-Swift.h" in the header files.
If you need Swift classes or protocols in the Obj-C code, you may forward declare them in the related Obj-C header. Here's more information about that:
When declarations in an Objective-C header file refer to a Swift class or protocol that comes from the same target, importing the generated header creates a cyclical reference. To avoid this, use a forward declaration of the Swift class or protocol to reference it in an Objective-C interface.
// MyObjcClass.h
#class MySwiftClass;
#protocol MySwiftProtocol;
#interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
#end
Also, please note, you may have issues with importing Swift Enums and Protocols and Classes into ObjC, so you may need to explicitly define items which you want to be available to ObjC code with #objc keyword.
And you won't be able to use Swift structs in Obj-C.
From Swift to Objective C you just have to use #import "ProjectName-Swift.h" on your Objective C classes that needs access to Swift code. There's no need to add on the bridging header file. For the other way Objective C to Swift then you need to declare in the bridging header file.

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.

Using CocoaPods and Obj-C Bridging Headers

When you include 3rd party Obj-C libraries and use the Obj-C Bridging Header, you are able to directly use the integrated libraries without the import statement.
//Bridging header
#import <Parse/Parse.h>
#import <Bolts/Bolts.h>
//Now, within the project, I can make calls to the Parse library without imports.
However, if I include 3rd party Obj-C libraries using CocoaPods, this doesn't seem to be the case. I needed the import statement for each file to use the library.
Is there a way to rectify this?
You can achieve this using Bridging Header similarly as you had it without Pods, follow the steps.
Keep your bridging header at the project home directory.
ie., if your project name is SampleProject. Usually you keep your Bridging header at the position SampleProject->SampleProject->Bridging-Header.h
But now, keep it at SampleProject->Bridging-Header.h
Get your pods library header file's path and import it inside your Bridging-Header.h file as usually.
(Refer this link for details.)
Remaining things will work like a charm.

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