Category: React Native

Create a custom React Native module that exports Constants (with Swift)

In this post, we will create a simple React Native module using Swift. The module will return information about the current device and expose it to JavaScript. First, we create our Swift class, RNDevice.swift. This class will is responsible for returning information that describes the current device. This information is obtain using the UIDevice class. [code language=”Java”] import UIKit @objc(RNDevice) class RNDevice: NSObject { @objc func constantsToExport() -> NSObject {...

Create a React Native custom module (with Swift)

Here is a quick and dirty example of how one can expose a custom module to React Native using Swift. Product-Bridging-Header.h – Bridge header file [code language=”C”] #import "RCTBridgeModule.h" [/code] MySwiftThingy.m – Register Swift code with the React Native Bridge [code language=”C”] #import "RCTBridgeModule.h" @interface RCT_EXTERN_MODULE(MySwiftThingy, NSObject) RCT_EXTERN_METHOD(callbackMethod:(RCTResponseSenderBlock)callback) RCT_EXTERN_METHOD(simpleMethod:(NSString *)message) @end [/code] MySwiftThingy.swift – Custom Swift component [code language=”Scala”] import Foundation @objc(MySwiftThingy) class MySwiftThingy: NSObject { @objc func callbackMethod(callback: RCTResponseSenderBlock)...