After the WindVane miniapp container is integrated into a ReactNative app, you can use WindVane in the ReactNative app to load a miniapp.
Procedure
If you want to integrate the WindVane miniapp container into a ReactNative app, you must integrate the WindVane SDK into a native app, and use the native module feature of ReactNative to call the methods provided by the WindVane SDK in the native app to query and open a miniapp. Perform the following operations:
Integrate the WindVane SDK into a native Android or iOS app.
Define a native module in the Android or iOS app, and then define and implement the corresponding methods.
Import the defined native module to the JavaScript environment, and then call the corresponding methods in the native module.
Step 1: Integrate the WindVane miniapp container into a native app
For more information about how to integrate WindVane into a native app, see the following topics:
Step 2: Define and implement a native module in the native app
Android
Create a Java class that inherits
ReactContextBaseJavaModule
, and implement thegetName()
method in which a native module is defined.Sample code:
public class EMASMiniAppModule extends ReactContextBaseJavaModule { @NonNull @Override public String getName() { return "EMASMiniAppModule"; } }
Use the
@ReactMethod
annotation to define the methods that can be called in JavaScript.Sample code:
@ReactMethod public void openMiniApp(String appId, Promise promise) { IMiniAppService miniAppService = ServiceManager.getInstance().getService( IMiniAppService.class.getName()); if (miniAppService != null) { miniAppService.openMiniApp(this, appId, null, new OnOpenMiniAppListener() { @Override public void onOpenMiniApp() { //start open } @Override public void onOpenSuccess(String appId) { WritableMap result = new WritableNativeMap(); result.putString("success", "true"); promise.resolve(result); } @Override public void onOpenFailed(String appId, int errorCode) { WritableMap result = new WritableNativeMap(); result.putString("success", "false"); result.putString("errorCode", errorCode + ""); promise.resolve(result); } }); } }
Create a Java class that inherits
ReactPackage
, and implement thecreateNativeModules
method in which a native module class is registered.Sample code:
public class EMASMiniAppPackage implements ReactPackage { @NonNull @Override public List<NativeModule> createNativeModules( @NonNull ReactApplicationContext reactApplicationContext) { List<NativeModule> modules = new ArrayList<>(); modules.add(new EMASMiniAppModule()); return modules; } @NonNull @Override public List<ViewManager> createViewManagers( @NonNull ReactApplicationContext reactApplicationContext) { return Collections.emptyList(); } }
Register the created
ReactPackage
class in the getPackages() method of the Application class.Sample code:
@Override protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); // Packages that cannot be autolinked yet can be added manually here, for example: packages.add(new EMASMiniAppPackage()); return packages; }
iOS
Create a class that implements the RCTBridgeModule protocol.
Sample code:
#import <React/RCTBridgeModule.h> @interface EMASMiniAppModule : NSObject <RCTBridgeModule> @end
Use the RCT_EXPORT_MODULE macro to export the native modules that can be called in JavaScript.
Sample code:
#import "EMASMiniAppModule.h" @implementation EMASMiniAppModule RCT_EXPORT_MODULE(); @end
Use the RCT_EXPORT_METHOD macro to define and export methods that can be called in JavaScript.
Sample code:
#import "EMASMiniAppModule.h" @implementation EMASMiniAppModule RCT_EXPORT_MODULE(); RCT_REMAP_METHOD(openMiniApp, openMiniAppWithAppId:(NSString *)appId openMiniAppWithResolver:(RCTPromiseResolveBlock)resolve openMiniAppWithRejecter:(RCTPromiseRejectBlock)reject) { id<EMASMiniAppService> miniAppService = [[EMASServiceManager sharedInstance] serviceForProtocol:@"EMASMiniAppService"]; if (miniAppService) { [miniAppService openMiniApp:appId openConfiguration:nil completionBlock:^(int resultCode, NSDictionary * _Nonnull resultDict) { resolve(nil); }]; } } @end
Step 3: Call the native module in JavaScript
Import the
NativeModules
interface to the JavaScript file.Sample code:
import { NativeModules } from 'react-native';
Use the NativeModules interface to obtain the native module defined in the native app.
Sample code:
const EMASMiniAppModule = NativeModules.EMASMiniAppModule ? NativeModules.EMASMiniAppModule : new Proxy( {}, { get() { throw new Error(LINKING_ERROR); }, } );
Call the methods defined in the native module.
Sample code:
EMASMiniAppModule.openMiniApp(`${appId}`).then((value) => {});