Import another package along with a SPM Library - swift

Imagine this scenario when your App project uses two custom libraries
.xcworkspace
|- App.xcodeproj
|- LibraryA.xcodeproj
|- LibraryB.xcodeproj
And since in this case we are working with Xcode frameworks one could add the following to the LibraryB header file:
#import <LibraryA/MyLibraryA.h>
and now not only there is no need to import LibraryA in the source od LibraryB, but also every time you import LibraryB inside your App source files, the LibraryA gets imported along with it, which can be very convenient.
The same applies to the default Foundation import one can find in an Xcode framework header:
#import <Foundation/Foundation.h>
Now with SPM, I need to manually import Foundation everywhere I use it.
Q: Is this possible to achieve the same result when using SPM packages exclusively?
I haven't found any resources on this matter. Thanks!

To expose import to your whole package you can use #_exported attribute inside of any of your swift file:
#_exported import Foundation
More info about this in next discussion: https://forums.swift.org/t/package-manager-exported-dependencies/11615

Related

Using static C library in swift, cannot find module

I need to create a swift wrapper for a C library for use on both iOS and macOS.
I have added the .a to the frameworks list and include it in library search path. I have added the header file to project and added it to User header search paths and I have added a module.modulemap to the project as well. Looking like this:
module codinglibc [system][extern_c] {
header “codinglibc.h”
export *
}
But when I import the module in Swift:
import Foundation
import codinglibc
I get this error message: No such module 'codinglibc'.
The project is a Cocoa Framework and I have been following this guide: https://medium.com/swift-and-ios-writing/using-a-c-library-inside-a-swift-framework-d041d7b701d9
I have looked at a lot of stackoverflow answers but most have been solved by adding import paths, which I already have done and Xcode can find both the header file and the static library so that is not the issue.
So:
1. Have I done something obviously wrong which I have missed?
2. Should I use briding headers instead?
Edit: I tried enabling Allow Non-modular Includes In Framework Modules
still no success
The answer is pretty trivial, yet annoying.
If you add the module.modulemap in an Xcode project, Xcode will not register it as "to be imported", so what you need to do is to add the path to you module.modulemap file in the header includes.

Integrating Microsoft Cognitive SpeechSDK framework into a Swift app

I'm trying to integrate Microsoft Bing Speech API with SpeechRecognitionService into my Swift application. Unfortunately, the Microsoft SDK only supports Objective-C atm, so I get around by adding #import "SpeechRecognitionService.h" to the Bridging Header after importing the SpeechSDK.framework, but I got the file not found error.
What am I doing wrong?
EDIT:
I did try import SpeechSDK framework directly into the needed class before but it was not working.
In my case, I'm still using the Bridging Header in order to import the framework. #import "SpeechRecognitionService.h" didn't work but a slight change as below works for me.
#import "SpeechSDK/SpeechRecognitionService.h"
There is no need to add header to bridging header, you can simply import the framework. From apple docs:
Importing External Frameworks
You can import external frameworks that have a pure Objective-C
codebase, a pure Swift codebase, or a mixed-language codebase. The
process for importing an external framework is the same whether the
framework is written in a single language or contains files from both
languages. When you import an external framework, make sure the
Defines Module build setting for the framework you’re importing is set
to “Yes”.
You can import a framework into any Swift file within a different
target using the following syntax:
import FrameworkName
See also “file not found” in Bridging Header when importing Objective-C frameworks into Swift project by CocoaPod
In my case, I'm still using the Bridging Header in order to import the framework. #import "SpeechRecognitionService.h" didn't work but a slight change as below works for me.
#import "SpeechSDK/SpeechRecognitionService.h"

Objective-C Bridging Header for frameworks

I've made a framework that requires the sqlite3 framework. How do I add a Objective-C Bridging Header for my framework that imports sqlite3 into my Swift file?
I already have a bridging header file for my project, but not for my framework.
I found a Objective-C Bridging Header setting in the target Build Settings. It was hidden by default. Check All instead of Basic.
In recent Xcode versions this solution would give the error Using bridging headers with framework targets is unsupported.
The workaround I've been using is to make the C-header public in the file inspector and import it in MyFramework.h like this example:
#import <MyFramework/MyObjectiveC.h>
How to change the C-header to public
Open your C-header and view the inspector by clicking in the upper right corner. To view the file inspector, click the file icon in the upper right corner.
just import your sqlite3 framework in your objective-c bridging file. You then automatically can use it in Swift.
Apple Docs:
Interoperability is the ability to interface between Swift and Objective-C in either direction, letting you access and use pieces of code written in one language in a file of the other language. As you begin to integrate Swift into your app development workflow, it’s a good idea to understand how you can leverage interoperability to redefine, improve, and enhance the way you write Cocoa apps.
One important aspect of interoperability is that it lets you work with Objective-C APIs when writing Swift code. After you import an Objective-C framework, you can instantiate classes from it and interact with them using native Swift syntax.
EDIT: You even can import an Objective-C framework or Swift framework or a mixed-language framework just into your swift file with import yourFramework
Apple Docs:
Importing External Frameworks
You can import external frameworks that have a pure Objective-C
codebase, a pure Swift codebase, or a mixed-language codebase. The
process for importing an external framework is the same whether the
framework is written in a single language or contains files from both
languages. When you import an external framework, make sure the
Defines Module build setting for the framework you’re importing is set
to Yes.
You can import a framework into any Swift file within a different
target using the following syntax:
SWIFT
import FrameworkName
You can import a framework into any Objective-C .m file within a
different target using the following syntax:
#import FrameworkName;

how to import bugsense in Swift

i can find its old implementation that is
#import <BugSense-iOS/BugSenseController.h>
and
[BugSenseController sharedControllerWithBugSenseAPIKey:#"123456" userDictionary:nil sendImmediately:YES]
and i am working in the Swfit Language
Import doesn't support dash (-)
can anyone update me how to import it in Swift, i also checked it on their bugsense documentation but couldn't find any updates
Create an ObjC bridging header help you to import it. First add the framework to your app directory and Xcode will ask you to create the bridging header.
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

How does importing within Swift work?

If I want to use Quartz and I type
import QuartzCore
for some reason, it works. If I type anything else, it doesn't work. When I check the documentation (Command click), it is a blank header file. How do I import headers?
As per the docs, import works for any Objective-C framework (or C library) that is accessible as a module.Objective-C frameworks vend APIs in header files. In Swift, those header files are compiled down to Objective-C modules, which are then imported into Swift as Swift APIs.