SWIFT_OBJC_BRIDGING_HEADER in Swift Package Manager - ios

I am trying to utilize an Objective-C framework inside the Swift package. For this, I would need to import the headers something like
#import<Framework/header.h>
Traditionally this can be achieved via bridging header. How can I set the bridging header via package manager settings?

This is bit tricky. You cannot import Objective-C framework's header files directly into SPM as you cannot write import statement such that #import<Framework/header.h>
The only feasible way is, write swift interface for your header files in Framework and then you would be able to import all methods. It can be done with module map as referred here

Related

#testable import and Bridging header don't work simultaneously

I have Test target for my framework project which include:
Obj-C tests
Swift tests with #testable import to test internal classes and functions
Some utils written in Obj-C for my tests which should be used in both 1) and 2)
Since I want to use Obj-C utils I should add Bridging header to my test target to have access from swift sources. The problem is that #testable import doesn't work with Bridging header simultaneously: as long as I don't use Bridging header I have access to all internal API from my swift test sources, but when I add Bridging header I have only access to public API.
Has anyone experienced that? Is #testable directive implemented using Bridging header which makes using both simultaneously impossible? Thoughts on how to resolve the issue?
UPDATE:
Apparently problem appears when you're importing the same framework as #testalbe in your swift code and using #import in Obj-C in your bridging header (directly or in one of included headers). I can manage my code to avoid importing framework simultaneously from Bridging header and swift, though general problem is not resolved

Can't reference framework .h header files from my Swift controller

I'm trying to import a framework made up of Objective-C header files.
I've created a bridging file but when I try to import a header the compiler doesn't recognise it.
This is error Xcode is showing:
Also if I try to reference the Framework in my Swift file as import ArcGIS it doesn't recognise that either.
What am I doing wrong?
You dont have to import ArcGIS header file. Guess you have used Carthage or CocoaPods. The only thing you need is to import ArcGIS
Check the documentation here
https://developers.arcgis.com/ios/swift/guide/install.htm

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.

Can't import CommonCrypto in mixed language framework

I'm building a mixed language framework. I mainly have Swift files, and a few Objective-C ones.
One of the Objective-C files is a crypto class that uses CommonCrypto.
It seems that I can't import it for some reason, even though that I can import it in a Objective-C framework.
Can someone explain to me why that is?
All the other solutions that I found talk about how to use CommonCrypto in Swift when I need to use it in Objective-C in a Swift framework.
P.S:
I tried adding the import in the umbrella header file like so:
#import <CommonCrypto/CommonCrypto.h>
the error: Include of non-modular header inside framework module 'name of header'
This answer did not fix the problem: answer
I've encountered this very problem myself. Here's how you resolve it:
Create a module map file (here's my file).
Copy the latest CommonCrypto.h header.
Create a directory CommonCrypto for both these files.
Copy the directory (via drag-and-drop) to your project.
Add the directory path under SWIFT_INCLUDE_PATHS for your target framework.
This should allow you to use import CommonCrypto wherever you want (for Swift, not Objective-C).
Edit: Seems like I misread the question initially. You want to use CommonCrypto in Objective-C and then use that from Swift. Here's some advice: don't #import CommonCrypto in your public headers, but rather just internally. Wrap all your crypto-structures so that there's no public dependency for CommonCrypto whatsoever, and then just use it from Swift via the default bridging procedure.

What is an alternative to pch in swift?

I was wondering what could be used instead of pch in swift.
Is there any alternative to pch or way to get rid of import in swift so that we don't need to do this for all classes.
I don't want to carry around/import all the time. What could be the best substitute of pch in swift.
You cannot define "Macros" in swift, so there is no ".pch" file for swift.
Also, Swift doesn't have separate "Header (.h) and Implementation (.m)", so there is no need of predefined headers which are needed to compile.
For more information refer to this question, Why .pch file not available in swift?
Class Import Problem:
In Swift, you don't need to write "import" statement for your project classes everywhere. You can access the project classes just by creating their objects.
Pods:
If you are using pods then it's necessary to import frameworks in
every class where you are using.
Third Party Swift Frameworks:
For external Frameworks/Modules, you can create a bridging header file
and replace the below line with your framework/module name:
#import ModuleName;
Reference: Implicitly import specific Swift module
There is no Swift equivalent for Objective-C .pch files.
Module is alternative for it.

Resources