Open wandonye opened 7 years ago
+1
Unfortunately, currently it is not possible to use Swift in a static library. react-native-create-library
would have to create a dynamic framework, what would add a lot of overhead to the startup time of your app if you link a lot of dynamic libs. Furthermore currently react-native link
does not support dynamic frameworks.
That said you can still use Swift in a React Native library, if you integrate the library via CococaPods. CocoaPods should automatically create a dynamic lib from your React Native library and setup your Xcode project the right way, like e.g. native-navigation (https://github.com/airbnb/native-navigation) does as it is Swift only too.
The cool thing is that if you use react-native-create-library
to bootstrap the library you should also get a podspec file you can use to integrate via CocoaPods.
I found out interesting fact. If I add even empty swift file to main project I get this bunch of settings:
then I create bridging file SomeName-Bridging-Header.h and setup property Objective-C-Bridging-Header to refer to this file. And after that I can use swift code in the static library in the way how it describes here https://facebook.github.io/react-native/docs/native-modules-ios.html (Making Objective-C Manager file for exposing Swift module from the library) It builds and links.
Im not so good with Xcode and don't know if there is a way to get those bunch of settings without creating empty swift file.
Funny :) but I found description of the same hack on the bottom of the doc I put the link before https://facebook.github.io/react-native/docs/native-modules-ios.html
So it means there are no other ways to get those settings in Xcode than to add a empty swift file to main project. And this will allow to use swift in static libraries.
@Elf2707 Im trying to do the same that you did however i get this error in my swift code 'AVStreamingKeyDeliveryPersistentContentKeyType' is only available on iOS 9.0 or newer is there a way to set the iOS deployment version in the static library
I create library using this package and inside it I did the following:
objective-c
files to swift
fileobjective-c
file to export the method I implemented in the swift
file (currently only 1)libName-Bridging-Header
fileThe steps above were inside the newly created library. When I build it - it failed. But according to issue #87, the library can't be "standalone" project.
I linked the library to the main app (written in react-native v0.57.8
):
Libraries
group of the main applibrary.a
is in the Link Binar With Libraries' list in
Build Phases` sectionBut the build keep failing :( The settings that @Elf2707 mentioned are not visible to me in the main app:
Is the only option for me to write library in iOS native is to use objective-c
?
You can use my https://github.com/iyegoroff/make-react-native-package CLI tool to scaffold a react-native package with Swift/Kotlin/Typescript
I added a swift class, then it complains:
when I click run.