React Native is an Objective-C application framework that bridges JavaScript applications running in the JSCore JavaScript engine to iOS and Android native APIs.
In theory, you write your application logic in JSX and ES6/7 and transpile it to JavaScript, and the application framework loads all that as a bundle.
In practice, you will want to expose your own custom native code to your JavaScript application. You may want to provide access to 3rd party library APIs or iOS framework features that arenโt exposed (yet) by React Native.
NEW RESEARCH: LEARN HOW DECISION-MAKERS ARE PRIORITIZING DIGITAL INITIATIVES IN 2024.
React Native is written in Objective-C, but we can write modules in Swift and expose them to our applications. The documentation on the React Native site briefly talks about โExporting Swift,โ but is thin on the details of doing much of anything in Swift. In this article, weโll do a deeper dive into interfacing Swift to JavaScript.
It is assumed you already have React Native and its prerequisites installed on your system. You can find the code used in this article in this GitHub repository: https://github.com/ModusCreateOrg/swift-modules-for-react-native.
Create a React Native project and open it in Xcode:
$ react-native init SwiftBridge && cd SwiftBridge $ ls android/ index.ios.js node_modules/ index.android.js ios/ package.json $ ls ios SwiftBridge/ SwiftBridge.xcodeproj/ SwiftBridgeTests/ $ open ios/SwiftBridge.xcodeproj
Click the run button in Xcode and see that the project builds and runs.
At this point, we know that if the application fails to compile or run, it is something weโve done to the project at fault.
Weโll start by implementing the CalendarManager sample code from the React Native docs and see that it works.
First, we need to add our Swift source file. Right click on SwiftBridge and select โNew Fileโฆโ:
Choose iOS and Swift File from the โChoose templateโ dialog:
Choose a filename for the file:
You will be asked if you would like to configure an Objective-C bridging header. We will need this, so click on the Create button:
At this point, Xcode has created two files in the project:
We copy the CalendarManager.swift example code from the React Native docs page and paste it into our CalendarManager.swift file:
// // CalendarManager.swift // SwiftBridge // // Created by Michael Schwartz on 12/11/15. // Copyright ยฉ 2015 Facebook. All rights reserved. // import Foundation // CalendarManager.swift @objc(CalendarManager) class CalendarManager: NSObject { @objc func addEvent(name: String, location: String, date: NSNumber) -> Void { // Date is ready to use! } }
Unfortunately, we have to provide an Objective-C file that exposes our Swift to the React Native Objective-C framework. Create the file โCalendarManageBridge.mโ by selecting โNew Fileโ as before and choose Objective-C File this time:
This time you will be presented with a โChoose options for your new fileโ dialog:
Enter โCalendarManagerBridgeโ in the File text field and click the Next button. Click the Create button on the next dialog:
The file is added to your project.
Copy and paste the code from the React Native docs page to this file:
// // CalendarManagerBridge.m // SwiftBridge // // Created by Michael Schwartz on 12/11/15. // Copyright ยฉ 2015 Facebook. All rights reserved. // #import <Foundation/Foundation.h> // CalendarManagerBridge.m #import "RCTBridgeModule.h" @interface RCT_EXTERN_MODULE(CalendarManager, NSObject) RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(NSNumber *)date) @end
Finally, we edit the SwiftBridge-Bridging-Header.h file and copy the two lines from the React Native docs page there:
// // Use this file to import your target's public headers that you would like to expose to Swift. // // CalendarManager-Bridging-Header.h #import "RCTBridgeModule.h"
Click on the run button in Xcode again and the project should run. If not, you did something wrong in the above steps.
In theory, we now have our Swift CalendarManager class exposed to JavaScript. It should appear to JavaScript as React.NativeModules.CalendarManager. Use the editor of your choice to add the following line to index.ios.js, just after the line that required react-native:
var React = require(โreact-nativeโ); // after this line console.dir(React.NativeModules.CalendarManager); // โ add this line
When we run the project from Xcode with this line, we will get a red screen error:
It seems console.dir() is only present if weโre debugging via Chrome. Click the Dismiss (ESC) link on the red screen, then from the Simulatorโs Hardware menu, choose Shake Gesture and choose โDebug in Chromeโ from the action sheet that appears in the emulator window:
You should see a new window or tab in Chrome that looks something like this:
After hitting Command-Shift-J as the page suggests, you should see something like this:
Note that the console.dir() did work and you can expand the Object to see that our React.NativeModule.CalendarManager object is exposed to JavaScript and it contains the addEvent() method as we expect.
Letโs implement some code in the addEvent() method to see that we can call it from JavaScript and access the arguments passed to Swift. Edit CalendarManager.swift so it looks like this:
// // CalendarManager.swift // SwiftBridge // // Created by Michael Schwartz on 12/11/15. // Copyright ยฉ 2015 Facebook. All rights reserved. // import Foundation // CalendarManager.swift @objc(CalendarManager) class CalendarManager: NSObject { @objc func addEvent(name: String, location: String, date: NSNumber) -> Void { NSLog("%@ %@ %S", name, location, date); } }
All thatโs really changed is the NSLog() call to dump the passed variables. Letโs also add a call to the addEvent() method to index.ios.js, just after the console.dir():
React.NativeModules.CalendarManager.addEvent(โOneโ, โTwoโ, 3);
When we run this, and the application crashes. There is an error reported in both Chrome Dev Tools and Xcode and in the simulator.
Note that the NSLog() did work, but the React Native framework displayed the red screen.
The fix for this is to add โnonnullโ to the CalendarManagerBridge.m file:
RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(nonnull NSNumber *)date)
With this change, the app works without any errors. We can also see in the Xcode console the NSLog() output:
(Note: I created a GitHub issue about this problem with the React Native documentation page and it has been fixed).
We have verified we can access the arguments passed to our Swift method from JavaScript.
We cannot simply return values to JavaScript because React Nativeโs JavaScript/Native bridge is asynchronous. That is, you have to implement your Swift method with a callback parameter and call it with a callback function from JavaScript, or you may implement events.
Letโs examine the callback mechanism first. Change the RCT_EXTERN_METHOD line in CalendarManagerBridge.m to read:
RCT_EXTERN_METHOD(addEvent:(NSString *)name location:(NSString *)location date:(nonnull NSNumber *)date callback: (RCTResponseSenderBlock)callback);
This adds a 4th parameter to the method, a callback function. In CalendarManager.swift, we need to alter the the addEvent() method:
@objc func addEvent(name: String, location: String, date: NSNumber, callback: (NSObject) -> () ) -> Void { // Date is ready to use! NSLog("%@ %@ %@", name, location, date) callback( [[ "name": name, "location": location, "date" : date ]]) }
What this version of addEvent() does is call the callback() method with a JavaScript Object that has the argument names/values as key/value pairs. In Swift, we create an NSObject with the [ key: value ] syntax. The argument to the callback from Swift is an array of argument values. In this case, we have just the one Object.
We need to modify the JavaScript code in index.ios.js to pass a callback. It should look like this:
React.NativeModules.CalendarManager.addEvent("One", "Two", 3, function(o) { console.log(โIn Callbackโ); console.dir(o); });
When we run this version of the code in the simulator, this is displayed in the JavaScript debugger console:
In order to use events, we need to modify the ‘CalendarManager-Bridging-Header.h’ file to import additional headers from React Native. The file should read:
// // Use this file to import your target's public headers that you would like to expose to Swift. // // CalendarManager-Bridging-Header.h #import "RCTBridge.h" #import "RCTBridgeModule.h" #import "RCTEventDispatcher.h"
An RCTBridge instance contains an eventDispatcher that we can use to send events to JavaScript from Swift. In order to get this instance, we can have one synthesized for us in our CalendarManager class. We can also verify that it is synthesized by using NSLog() to dump its value.
Modify the classโ code in CalendarManager.swift so it looks like this:
class CalendarManager: NSObject { var bridge: RCTBridge! // this is synthesized @objc func addEvent(name: String, location: String, date: NSNumber, callback: (NSObject) -> () ) -> Void { // Date is ready to use! NSLog("Bridge: %@", self.bridge); NSLog("%@ %@ %@", name, location, date) callback( [[ "name": name, "location": location, "date" : date ]]) } }
There is the bridge member that will be synthesized and in the addEvent() method there is a call to NSLog() to print the value of the bridge. The value printed should be some hex number thatโs the address of the bridge instance.
When we run the code, we can see that the bridge member is synthesized:
Note that the code uses NSLog() instead of print(). NSLog is synchronized and works better with threading. It also prints a timestamp and prints to the device console when running on device.
Ultimately debug logging should be wrapped by some other means so the printing can be disabled or directed as you want.
Modify the CalenderManager class one more time so it looks like this:
class CalendarManager: NSObject { var bridge: RCTBridge! // this is synthesized @objc func addEvent(name: String, location: String, date: NSNumber, callback: (NSObject) -> () ) -> Void { // Date is ready to use! NSLog("Bridge: %@", self.bridge); NSLog("%@ %@ %@", name, location, date) let ret = [ "name": name, "location": location, "date" : date ] callback([ret]) self.bridge.eventDispatcher.sendAppEventWithName("EventReminder", body: ret) } }
The sendAppEventWithName() method takes an event name and an arbitrary object that is sent to the JavaScript event handler. In the code above, weโre assigning the NSObject with arguments as key/value pairs to a variable and using it to pass to both the callback() and the event argument.
Modify the JavaScript code near the top of index.ios.js so it reads:
var React = require('react-native') console.dir(React.NativeModules.CalendarManager) var subscription = React.NativeAppEventEmitter.addListener( 'EventReminder', (reminder) => { console.log(โEVENTโ) console.log('name: ' + reminder.name) console.log('location: ' + reminder.location) console.log('date: ' + reminder.date) } ); React.NativeModules.CalendarManager.addEvent("One", "Two", 3, function(o) { console.log('In Callback') console.dir(o) })
Weโre really just adding the subscription logic before calling addEvent(). When we run this version of the code, we see the expected output in the Chrome console:
Finally, letโs export some constant members from the CalendarManager swift class to JavaScript. Add this code to the CalendarManager class before the closing brace:
@objc func constantsToExport() -> NSObject { return [ "x": 1, "y": 2, "z": "Arbitrary string" ] }
When we run the project and expand the first Object printed in the Chrome console, we see our constants:
In conclusion, we now have patterns to interface Swift native code to our JavaScript in React Native. From here we can implement our application logic in either language, as appropriate.
Mike Schwartz
Related Posts
-
React Navigation and Redux in React Native Applications
In React Native, the question of โhow am I going to navigate from one screen…
-
Using ES2016 Decorators in React Native
*picture courtesy of pixabayDecorators are a popular bit of functionality currently in Stage 1 of…