Use ObjectiveC from Swift in framework, without making ObjC code public - ios

I have framework that is written in ObjC and Swift, I want to use ObjC classes in Swift. Apple says "In your umbrella header file, import every Objective-C header you want to expose to Swift". But everything included to umbrella becomes public. Is there any way to use ObjC without making it public?

The umbrella header is not your framework header, it's the one named with "-Swift.h" in it. You don't need to include that header in your main framework header.

Related

How to use Swift framework in Objective-c app

I would like to use a Swift library in my Objective-C IOS app. I have the Swift library setup as a separate project, which builds fine in XCode. I can drag the generated Swift framework from the Swift project into the "Frameworks, Libraries and embedded content" list of my Objective-C project target. It appears with an "Embed & Sign" label.
The Apple description here: https://developer.apple.com/documentation/swift/importing-swift-into-objective-c states that
You can work with types declared in Swift from within the Objective-C
code in your project by importing an Xcode-generated header file. This
file is an Objective-C header that declares the Swift interfaces in
your target, and you can think of it as an umbrella header for your
Swift code. You don’t need to do anything special to create the
generated header—just import it to use its contents in your
Objective-C code.
When I look inside the swift .framework file, I can see the header file there. But when I import it in one of my Objective-c .m files, then the compiler says that the file is not found. I have tried both the
#import "Starscream-Swift.h"
and the
#import <Starscream/Starscream-Swift.h>
syntax.
How can I convince XCode to use the header file from the Swift framework? Do I need to copy that header somewhere, maybe? And what else do I need to think about to use a Swift library from an Objective-c IOS app?
I am using XCode version 13.4.1.
By the way, the Swift library I am trying to use is Starstream.
Xcode-generated header file is required when your own project has Objective-C and Swift classes mixed. When dealing with frameworks you either include the framework's umbrella header like this:
#import "SwiftFramework/SwiftFramework.h"
Or, more preferably, you import it with #import expression:
#import SwiftFramework;
Be advised, that only part that is exposed to Objective-C runtime is accessible from this framework (i.e. public/open classes inherited from NSObject or some other Cocoa classes)

How to create a framework with Objectivec files and Swift files

I have a Xcode project with Obiective-c files and swift files, Now i want to create a framework with both classes, Is this possible ?
Yes, this is straightforward and the process is documented by Apple. Please read "Swift and Objective-C in the Same Project" with thought and you'll be wiser (the section "Importing Code from Within the Same Framework Target" covers importing Objective-C in Swift from the same target, and the other way). Briefly…
For framework targets you don't need to create a bridging header to make your Objective-C importable to Swift, you just need to #import those headers you want visible to Swift in the framework's umbrella header. For that to work, the header needs to be marked public.
For Swift to be importable in Objective-C in the same framework target, make sure "Defines Module" is toggled on for the target, and in the files where you need to reference the Swift types, do an import in the style #import <ProductName/ProductModuleName-Swift.h>. If I recall this right, the Swift types / methods / properties you want to access from Objective-C will need to have been declared public for it to be accessible to Objective-C code even if it's in the same target.

How to call Objective-C libraries in dynamic framework Swift?

I have created a dynamic framework with Swift language. In this framework I am using mailcore2-ios that was build by Objective-C code. So how can I use it in swift file of my framework?
I try "public" and add a header .h file to umbrella but it still not work, have so much error for other header file as the image what I attach.
Xcode does not allow the use of bridging headers in framework targets. You should use the methods described here : http://iosdeveloperzone.com/2014/10/03/using-commoncrypto-in-swift/

Adding Objective C framework inside Swift Framework

I am writing a Cocoa Framework for iOS in Swift language. I need to add a third party framework (written in Objective-C) inside this framework. I have added headers to the bridging file. But when i build the project i get the following error:
"using bridging headers with framework targets is unsupported"
You should use import ObjcFrameworkName instead of using bridging header.
To make this possible objc framework must contain .modulemap file with exported module name and umbrella header for all public headers of this framework.

Purpose of Objective-C bridging header even when Swift is imported to Objective-C

The following was the observation made while trying to understand the interoperability with Swift and Objective-C:
I tried to use Swift class in my Objective-C project. Xcode asked to create a bridging header and the same was created. I suppose this bridging header is the Objective-C bridge header where we write the import statement for the Objective-C class or framework which need to be exposed to the swift calls. My plan was to use the swift class within Objective-C. Therefore I removed the bridging header along with the Objective-C Bridging Header value form the Swift Compiler – Code Generation section of build settings tab. When I build the application the compilation failed with message:
/Users/developer/Desktop/workspace/samples/SampleObjectiveC/SampleObjectiveC/AppDelegate.m:23:4: Receiver type 'NewSwift' for instance message is a forward declaration
NewSwift is my Swift class name which I was using in the Objective-C class.
When I added the bridging header file back and added the Objective-C Bridging Header the compilation was successful.
The use of bridging header when trying to expose Objective-C api to Swift could be understood.
But in our case when we are trying to expose Swift class into Objective-C what is the purpose of the Objective-C bridging header?
I would like to understand what is actually happening behind the scene?

Resources