Can't use Objective C framework imported within Bridging Header - ios

Usually I am using CocoaPods (with use_frameworks!) in order to manage my frameworks, but this PDF library (FastPdfKit) does not support CocoaPods, so i added it manually in "link binary with libraries" along with its dependencies. After that I let xcode create a bridging header for me (I created a .m file, and xcode created a bridging header automatically).
I imported the header in the bridging file like this #import <FastPdfKit/FastPdfKit.h>, but I can't acces any method/class from my swift files... (Use of undeclared type 'ReaderViewController' where ReaderViewController is a subclass of UIViewController in the framework).
I've also tried importing like this #import 'FastPdfKit.h" still gives the same errors.

To import Objective-C code into Swift from the same framework, go to Build Settings > Packaging and make sure the Defines Module setting for that framework target is set to “Yes”.

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)

Could not build Objective-C module, when using swift in objective-c module

In an iOS application I have a subproject (not cocoapods) in which I have included a swift file and ObjC file (that is used by the swift file). XCode automatically created a bridging file but could not build it because apparantly bridging is not allowed in a framework. The workaround that I used was to add the objective-c header to the umbrella file and it worked. Now I need to use a swift class from ObjC. I have define module to set to YES, the generated file Framework-Swift.h . But when I try to import it in objective-c i get
Could not build Objective-C module
The closest I got after some googleing was this answer:
Ah gotcha. It looks like you're building a mixed Swift & Objective-C
pod - if that's the case, Xcode will try to import
within the generated -Swift.h header.
You'll need to create the header manually and add imports for the
Objective-C classes that you want to expose to Swift.
CocoaPods generates an umbrella header automatically and imports it
within the .modulemap, but Xcode doesn't use that when generating the
-Swift.h header
But I am unsure what header needs to be created manually.
Any ideeas or pointer about using swift in an objective-c framework ? In both ways ?
I also had similar issue when using Swift pods in my Swift project containing several targets. No Objective-C code at all. I tried to clear build folder, pods cache, derived data - nothing worked.
Solution:
Open the Build Settings for a target that contains your module code. Set the "Install Objective-C Compatibility Header" to "No"
There's a great and simple article that wraps up this case:
DEFINES_MODULE=YES
To use ObjC classes in Swift, create a bridging header and specify path to it in the build settings
To use Swift classes in ObjC, #import <ModuleName/ModuleName-Swift.h> in your *.m file (use #class SwiftClass; forward declaration in *.h file, if needed)
For swift classes and their members to be visible to objc
inherit your class from NSObject
prepend it with #objcMembers
make both the class and its members public

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

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".

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

Swift Import Obj-C Framework

I am having trouble importing a Obj-C Framework into a Swift Project.
Beginning with an empty swift project here is everything I did:
Drag and drop the Tesseract framework into XCode (copy items if needed was checked)
Drag and drop a random .m file into XCode
XCode generated a Bridging Header File for me
Deleted random .m file, leaving bridging header file
Added import statement to the bridging header file
I dont have enough reputation to add pictures but here are links to various parts of my project:
Project File Structure
Project Build Phases
There is a single line of code in the Bridging Header:
#import <TesseractOCR/TesseractOCR.h>
In Build Settings the Obj-C Bridging Header setting is set to:
Swift OCR Test/Swift OCR Test-Bridging-Header.h
In Build Settings the Framework Search Paths setting is set to:
$(inherited) $(PROJECT_DIR)
I receieve a bunch of Apple Mach-O Linker Errors when I build the project. Could someone offer a suggestion about how to get the project set up properly?
Thanks
You need to add libstdc++.dylib to the Linked Frameworks and Libraries in the General tab for your project.

Resources