Swift framework with Objective-C Static library included - swift

I need to create a framework in swift, that will internally use a static library. It would be great if this library was only visible internally in this framework. I have added all the files of this library to the framework, and I have created a module.modulemap file. This file looks like this:
framework module MySDK {
umbrella header "MySDK.h"
export *
}
module BluFi {
header "/Users/homedudycz/Documents/Developer/MyApp/MySDK/blufiprotocol/DH_AES.h"
export *
}
And I can now use objects from this static library inside my framework, but there is a problem. When I try to import my framework in some app I get an error: Missing required module 'BluFi'.
What am I doing wrong? Is there some other way to import this static library inside my framework internally?
I have also created a sample project with libraries that I need added in this SDK. Please take a look, and please update path in module.modulemap - I don't know how to make it relative to SDK path :/
https://www.dropbox.com/s/rnh7y7zr63zillv/TestApp.zip?dl=1

Related

Add Swift Package to a custom framework

Pretty new to creating frameworks with SPM dependencies. So I made a new framework project, added some of my classes/files as well as a SPM dependency (CocoaLumberjack logger). Framework compiles fine.
When I look for my framework product that I'm planning on embedding into some other project I see that it is in my Products folder. Alongside with it I see CocoaLumberjack module. Inside of my framework there is not much beside the exec file.
When I try to embed my framework into some other projects. Nothing compiles because it says that CocoaLumberjack module is missing.
Does anyone know how to fix this? Am I missing an important step or soemthing?
Well, there are numerous isses you could have faced during importing framework itself. It also depends if you use framework as binary or source code. I assume you were using source code approach as you are the creator of framework. You can however check all approaches here: in this SO question . Lets look at all the steps you need to implement in order to successfully use framework with SPM dependencies in your swift project.
create SPM properly and also link all additional SPM dependencies tutorial here. Make sure all your classes, structs etc. and their coresponding initializer has correct access level property. If you plan to use them outside of the package, use public initializers..
2)Once you created you SPM package, link it to framework. For the sake of this answer I created testFramework and linked one of my custom SPM package called VodApiPackage . This package also contains dependency to another BaseTvApiServicePackage.
I also added TestPrinter file containing simple function for creating error declared in my SPM package. This function servers only for checking that everything is working properly and will be user later. It is also declared public.
import Foundation
import VodApiPackage
public struct TestPrinter {
public init () {}
public func makeTest() {
let x = VodApiError.customErr(msg: "testMsg")
print(x.localizedDescription)
}
}
Open your project and make link to framework, you can also check this nice tutorial. The most important step from tutorial is step 5 and 6. Where you drag .xcproj into your project and link libraries and framework
make sure your library and SPM dependencies are correctly linked in your project. Check sample project below.
Build and test using your framework and its packages:
import UIKit
import testFramework
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
testmodel()
TestPrinter().makeTest()
}
}

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.

Create a static library with React dependency

I created a library that uses the native navigation controller to navigate through react and native screens. The library is written in Swift and contains some objective-c code to setup the React bridge etc. Currently I can distribute this library through CocoaPods by creating a podspec and defining the React dependency there. However, this forces the user to setup React in their project through CocoaPods as well (like so: https://facebook.github.io/react-native/docs/integration-with-existing-apps.html). I'd like them to use the react-native link or manually linking option as well (like so: https://medium.com/#joshyhargreaves/adding-react-native-to-existing-ios-project-without-cocoapods-6f1ee9106009).
If I understand correctly I can create a static library and distribute that. So far I created this static library, added my mixed swift and objective-c code and tried to manually link it into my main project. This however produces an error in one of my classes inside my static library where I import React like so import React. Error is "No such module 'React'.
I updated the Header Search Paths of my static library to contain:
$(SRCROOT)/../react-native/React
and
$(SRCROOT)/../React
(react-native, React and my own lib are all inside the node_modules folder)
both set to recursive. Unfortunately it still doesn't find the module React. Does anyone know how to create a static library that contains a dependency with React?
The end goal would be to import this static library in my main project like import MyStaticNaivationLib in one of my viewcontrollers and subclass from a class that's defined in my static library.
My main project also uses https://github.com/rebeccahughes/react-native-device-info. This looks exactly what I want to achieve. It's a static library that has a dependency with React ("import RCTBridgeModule.h") which I can manually link in my main project. Only difference is that this project doesn't contain Swift code.
I wrote a custom module for the company, and you can view my documentation. This custom module have inner framework and I link this framework to the main project. In your custom_modulem do you have package.json with this?
"peerDependencies": {
"react": "16.8.3",
"react-native": "0.59.4"
}
https://github.com/Hardy1207/RNOkay

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;