javascript - React Native Swift callback -


i send string swift-implemented module react native , string result callback native module further use.

here's 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 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 swift: \(name)")     successcallback([name])   } } 

and whatever goes reactnative part:

// requiring swift module in react native var helloworldmodule = require('react-native').nativemodules.helloworldmodule;  ...  // using somewhere in 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 expect swiftcb: jadzia dax result not being passed over? did wrong in method definition in swift undefined? somehow got blind on problem :/ react native swift module callbacks unfortunately not covered in rn docs, too.

the rctresponsesenderblock callback follows node.js error first style i.e. first callback param holds error , second param holds result.

in example, returning value error, , no value result, hence undefined. should return nsnull error (first) parameter, , name second param e.g. [nsnull(), name].

this short blog post shows how using swift , react native. in blog post, see line 11 in myswiftthingy.swift.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -