React Native iOS 原生模块传递参数

问题描述

我正在编写一个包含 iOS 和 Android 原生模块的 NPM 库。重要的是我需要在启动之前将参数传递给本机模块。这适用于 Android:

package ...

import com.facebook.react.bridge.*

class MyNativeModule(reactContext: ReactApplicationContext,parameter: String) {

    override fun getName(): String {
        return "MyModule"
    }

    @ReactMethod
    fun retrieveParameter(promise: Promise) {
        promise.resolve(parameter)
    }

}

关闭自动链接时,库用户可以创建自己的 RN 包并使用以下内容设置参数:

class MyAppRNPackage(private val voizeCore: VoizeCore = VoizeCore.getInstance()) : ReactPackage {
    override fun createNativeModules(reactApplicationContext: ReactApplicationContext): List<NativeModule> {
        return arraylistof<NativeModule>(
            MyNativeModule(reactApplicationContext,"this is the parameter"),)
    }

    override fun createViewManagers(reactApplicationContext: ReactApplicationContext): List<ViewManager<*,*>> {
        return emptyList()
    }
}

在 iOS 中,本机模块看起来像这样:

// MyNativeModule.m
#import "MyNativeModule.h"

@implementation MyNativeModule

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(retrieveParameter:(RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject)
{
    resolve(???)
}

@end

但是如何为iOS模块设置一个参数呢?问题是在 iOS 中,无法手动注册原生模块。此外,只注册类,而不注册类实例

知道如何将参数从本机代码传递到 iOS 本机模块吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)