Hurobaki
Hurobaki

Reputation: 4058

React-native iOS Bridging module in Swift

I would like to create a native module in order to use it in my react-native project. The particularity is that I want to use the Swift language.

So I created a fresh RN project using react-native init command.

Here's my packages dependencies :

I created a directory inside my node_modules directory called react-native-hello-world in order to 1) train with a simple example 2) mock my future Npm package.

Inside this directory I created my HelloWorld.xcodeproj.

-- nodes_modules
   -- react-native-hello-world
      -- ios/
         -- HelloWorld/
            -- HelloWorld.xcodeproj
            -- HelloWorld/
               -- HelloWorld.swift
               -- HelloWorld-Bridging-Header.h

As I mentioned I want to create this module using Swift. So I used XCode to create a bridging header file as RN documentation says.

That's when my problems started ... When I add

#import <React/RCTBridgeModule.h>

In my HelloWorld-Bridging-Header.h file an error occurs and say 'React/RCTBridgeModule.h' file not found.

I tried to clean my project and even delete my derived data but I still have this error.

I even added in my target's header search path React's .h files.

$(SRCROOT)/../../../react-native/React  //recursive

But I still have this error ... I don't understand why, in my opinion I did everything I had to do right. so maybe we can't create a native module using Swift ?

Could someone tell me if I made a mistake or if they've already managed to tell me how to do it?

Some help would be really appreciated,

Thank you :)

Upvotes: 0

Views: 1603

Answers (1)

Hardy1207
Hardy1207

Reputation: 327

If you have only peer dependencies and try open HelloWorld.xcodeproj (As i understand that this is your module) it will not working, because you don't have node_modules into your custom_module folder.

  1. a) Added this module to your main project package.json

    "dependencies": { "react-native-hello-world": "file:custom_modules/RNHelloWorld", ... }

  2. npm install

  3. Open your PROJECT_NAME/ios/PROJECT_NAME.xcodeproj

  4. As you can see your RNHelloWorld added to your Libraries

  5. You can start working with your custom module here and error with RCTBridgeModule will dissapear, because you have libReact.a into your Link Library with Binaries

Upvotes: 2

Related Questions