Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Swift callback

I would like to send a String to a Swift-implemented module from React Native and then just get a String result as a callback from the native module for further use.

Here's what i've got:

//HelloWorldModule.m

#import "RCTBridgeModule.h"

@interface RCT_EXTERN_MODULE(HelloWorldModule, NSObject)

RCT_EXTERN_METHOD(sayHelloWorld:(NSString *)name callback:(RCTResponseSenderBlock *)successCallback)

@end

Alongside the Swift implementation:

//  HelloWorldModule.swift

import Foundation
import UIKit
import AVFoundation

@objc(HelloWorldModule)
class HelloWorldModule: NSObject {

  @objc func sayHelloWorld(name: String,  callback successCallback: RCTResponseSenderBlock) {
    NSLog("Log from Swift: \(name)")
    successCallback([name])
  }
}

and finally whatever goes into the ReactNative part:

// requiring the Swift module in React Native
var HelloWorldModule = require('react-native').NativeModules.HelloWorldModule;

...

// using it somewhere in the render function

render: function() {
       return (
        <Text>
          Hello World Module answers: {this.hwmExt("Jadzia Dax")}
        </Text>
       );
},

hwmExt: function(name) {
    return HelloWorldModule.sayHelloWorld(name, function(result) {
       var hwAnswer = "swiftCB: " + result;
       console.log(hwAnswer);
       return hwAnswer;
    });
}

The line console.log(hwAnswer); prints out what I expect it to be swiftCB: Jadzia Dax but the result is not being passed over? Did I do something wrong in the method definition in Swift as I always get undefined? Somehow got blind over this problem :/ React Native Swift module callbacks are unfortunately not covered in the RN docs, too.

like image 254
AirUp Avatar asked Jan 06 '16 05:01

AirUp


People also ask

What is a callback in React Native?

Callbacks are a great way of sending data back to React Native after running a native function. It’s not the only reason to use them though. In my example, I will expand upon my previous tutorial on native modules and we will create a function that returns a Boolean value whether or not the Audio files is playing by using a React Native callBack.

What is swift modules for React Native?

Swift Modules for React Native. 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.

How do I use React Native on iOS?

For the Google Chrome debugger, React Native runs inside the JS VM in Google Chrome, and communicates asynchronously with the mobile devices via WebSockets. At this point you have set up the basic scaffolding for your native module in iOS. Test that out by accessing the native module and invoking it’s exported method in JavaScript.

How do I create a calendar event in React Native?

In order to access your native module from JavaScript you need to first import NativeModules from React Native: You can then access the CalendarModule native module off of NativeModules. Now that you have the CalendarModule native module available, you can invoke your native method createCalendarEvent ().


1 Answers

The RCTResponseSenderBlock callback follows Node.js error first style i.e. the first callback param holds error and the second param holds result.

In your example, you are returning a value for error, and no value result, hence undefined. You should return NSNull as the error (first) parameter, and name as the second param e.g. [NSNUll(), name].

This short blog post shows exactly how to do this using Swift and React Native. In the blog post, see line 11 in MySwiftThingy.swift.

like image 94
cmd Avatar answered Oct 15 '22 14:10

cmd