NeilSMyers / NativeModuleDemo

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Write Native Modules with Swift and Kotlin

These instructions are a minimal step by step process. For context around the code snippets, please refer to the commit history, or the other branches for start and end points to compare. The code also contains comments for more info. To learn more about Native Modules you can read React Docs, How to write Native Modules, Sending Events, or Swift in React Native.

ios

  1. create swift class

  2. create bridging header #import "React/RCTBridgeModule.h"

  3. write swift

    import Foundation
    
    @objc (*YOUR MODULE*)
    class *YOUR MODULE*: NSObject {
    
      @objc
      func *YOUR FUNCTION*() {
        print("We're on our way buddy ole pal")
      }
    }
    
    
  4. create obj-c class

    #import "React/RCTBridgeModule.h"
    @interface RCT_EXTERN_MODULE(*YOUR MODULE*, NSObject)
    
    RCT_EXTERN_METHOD(*YOUR FUNC*)
    
    @end
    
  5. use in RN

    import { NativeModules } from "react-native"
    console.log(NativeModules.*YourModule*)

android

  1. add kotlin

    • app/build.gradle - apply plugin: "kotlin-android"
    • build.gradle
      • buildscript -> repositories - maven { url 'https://dl.bintray.com/kotlin/kotlin-eap'}
      • buildscript -> dependencies - classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21" (version will change)
  2. create kotlin class

  3. write kotlin

  4. create package class

  5. add instance of package class to the MainApplication getPackages() method packages.add(new *Your Package*());

  6. use in RN

    import { NativeModules } from "react-native"
    console.log(NativeModules.*YourModule*)

END

About


Languages

Language:Java 33.7%Language:Objective-C 26.4%Language:Starlark 10.3%Language:Kotlin 9.9%Language:JavaScript 9.1%Language:Swift 5.3%Language:Ruby 5.3%