react-native-create-library icon indicating copy to clipboard operation
react-native-create-library copied to clipboard

Support of Swift?

Open wandonye opened this issue 7 years ago • 7 comments

I added a swift class, then it complains:

Check dependencies
Swift is not supported for static libraries.

when I click run.

wandonye avatar Feb 26 '17 21:02 wandonye

+1

ananevam avatar Jun 01 '17 15:06 ananevam

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.

maicki avatar Feb 18 '18 18:02 maicki

I found out interesting fact. If I add even empty swift file to main project I get this bunch of settings: 2018-05-08 17 24 28

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.

Elf2707 avatar May 08 '18 14:05 Elf2707

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 avatar May 10 '18 21:05 Elf2707

@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

acollazomayer avatar Jul 24 '18 15:07 acollazomayer

I create library using this package and inside it I did the following:

  1. Migrate the objective-c files to swift file
  2. Create objective-c file to export the method I implemented in the swift file (currently only 1)
  3. Create libName-Bridging-Header file

The 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):

  1. The library is in the Libraries group of the main app
  2. library.a is in the Link Binar With Libraries' list in Build Phases` section

But the build keep failing :( The settings that @Elf2707 mentioned are not visible to me in the main app: image

Is the only option for me to write library in iOS native is to use objective-c?

b-asaf avatar Jan 16 '19 10:01 b-asaf

You can use my https://github.com/iyegoroff/make-react-native-package CLI tool to scaffold a react-native package with Swift/Kotlin/Typescript

iyegoroff avatar Aug 28 '19 20:08 iyegoroff