Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a React-Native framework using Swift

I have a react native component written in Swift, and I want to extract it as a framework so other project/people can use it easily.

First I created a new iOS project Foo, added a framework target, then added source files. Finally, I built it.

It failed because:

Bar.swift:[lineNumber]: Use of unresolved identifier 'RCTConvert' RNBarManager.swift:[lineNUmber]: Use of undeclared type 'RCTViewManager'

Attempt 1:

I ran react-native link, but nothing changed.

Attempt 2:

  1. Drag React.xcodeproj to Foo project and add libReact.a to Link Binary With Libraries.
  2. Add $(SRCROOT)/../node_modules/react-native/React to Header Search Paths.

But it doesn't work. The error still remains.

Attempt 3:

I was thinking maybe Swift doesn't recognize these files.

So I added Foo-Bridging-Headers.h like I did in a React-Native App, which contains the following code:

#import "React/RCTBridge.h" #import "React/RCTViewManager.h" #import "React/RCTUIManager.h" #import "React/UIView+React.h" #import "React/RCTBridgeModule.h" 

I also added Foo-Bridging-Headers.h to Build Settings->Swift Compiler - General->Object-C Bridging Header,

Still I got an error.

using bridging headers with framework targets is unsupported

It seems bridging headers are not allowed here.

Attempt 4:

I tried to add these imports to Foo.h,

but got an error again.

Foo.h:21:9: Include of non-modular header inside framework module 'Foo'

What should I do to compile successfully?

like image 652
XuDong Wu Avatar asked Mar 03 '17 04:03

XuDong Wu


People also ask

Can I use React Native with Swift?

The main difference between React Native and Swift is that React Native is best for cross-platform development, while Swift is for building iOS apps. Thus, you may develop an Android application in Java or Kotlin, for example, and choose Swift and Objective-C for iOS only.

Can I use Xcode for React Native?

Building a React Native app on iOS with XcodeYou can build a React Native app either using the React Native CLI, which allows you to run build commands from your terminal, or Xcode.

Does React Native use Objective-C or Swift?

Mostly React Native code is written with Objective-C because React Native core uses Yoga lib for layout that works better bridging with Objective-C because is written in C++. But how we can see on documentation we also can bridge our application with Swift to use these functionalities.

Is Swift easier than React Native?

Swift is future-proof and can be extended with new features as needed. Thus, such apps are typically easier to scale. Less coding. Swift is very concise, which means you need less code to perform the same task, as compared to React Native.


1 Answers

This seems like an issue with the cocoa pods required for linking React to your project.

Try running pod install in your project directory using terminal and then try to build.

like image 92
Siva Avatar answered Oct 03 '22 18:10

Siva