Mark Keane
Mark Keane

Reputation: 1044

Is it possible to use my Swift libraries in React Native?

I have decided to swap development over for my mobile app side project from native swift to cross platform React native. The immediate road block that occurs to me when I try and begin doing this is wondering how to access all the libraries I was using in Swift via Cocoapods in react native. For example I am using a ton of Amazon Web Services Swift libraries and the Chameleon Swift color library. Is there a way to convert all these swift libraries/make them accesible in React Native or will I have to look for react native/javascript versions of all these libraries?

Upvotes: 1

Views: 832

Answers (1)

Ryan Pfister
Ryan Pfister

Reputation: 3296

There are a lot of pre-built React Native components both written by the Facebook team and also the react native community. However, it seems from your question that you have some very specific native code that you will need to find a way to run in your future React Native application. The best answer I can give to your question "which libraries should I use if any of them" with the information provided is "it depends".

I have built a few React Native mobile applications for both Android and iOS, and functionality that would have existed in Swift in these applications can be sometimes written in JavaScript - for instance, calling a web service using the fetch api that is built into React Native. If the AWS service you are calling accepts a basic HTTP request, then that could be something that you could write yourself in JavaScript.

If you are doing something more complex such as downloading a file from S3, you may need to use something like the community built react-native-fetch-blob, which has worked well for when I needed to download files. You would need to provide whatever SWS Auth tokens in the request header that are needed.

Another option is to write your own Native Module. These are chunks of application logic that you call from javascript in react native. They are very powerful as they allow you to do essentially anything you could have done before in a straight native app. They are written in the native platform language, so Obj-C for iOS, Java for Android, (but if you are feeling ambitious you could probably write them in Swift - I haven't tried). These native modules could then call out to the AWS Swift libraries that you have that already exist. My instinct says this may be the solution that would fit your needs best. The obvious downside to native modules is that they are native, so you would need to write one for each platform you support (iOS and Android, presumably).

There is also the option of simply incorporating React Native into your existing application. The idea being you can slowly either build out new components of your app in React Native or rewrite current logic into React Native in a piecemeal approach, taking smaller bites at a time so the whole task seems less daunting.

Finally, the Chameleon Swift appears to be a UI library. It doesn't seem like you would want to keep using this library if you went with React Native, as React Native is the "front-end" of a mobile application (amongst other things). HOWEVER, if you felt so desirous, you could write a native module that, in theory, could call the chameleon library to provide you with things like hex values for colors. I don't know if this would work well and would likely not be very performant, so I couldn't recommend you attempt to continue to use this library in a straight react-native app (but for sure if you used a hybrid app, see above!). React native has it's own cool tools, however, that you can use to make your app more awesome (here, here, here).

Phew, I hope this was useful.

Upvotes: 1

Related Questions