如何从 React Native 调用 swift native 函数? [英] How to call swift native function from react native?

查看:57
本文介绍了如何从 React Native 调用 swift native 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 swift 类,我将它暴露给 react-native,在里面我有一个也暴露给 react-native 的函数.现在,当 react native 调用该函数时,它会在内部执行很多此类操作,但经过一段时间后,它会返回一个对象.

I have a swift class, which i am exposing to react-native, inside that i have a function that is also exposed to react-native. Now when react native calls that function it does a lot of this internally, but after some point of time it returns an object.

现在它将调用一个特定的函数来获取对象.我无法将参数更改为该函数.但我有另一个功能,我想返回到本机反应.我该怎么办.

Now it will call a specific function that will get the object. I cannot change the parameter to that function. But i have another function, to which i want to return to react native. How can i do it.

  func AckCallback(response:APIResponse) -> Void
  {
    print(response)  

  }

对于此函数,我无法更改参数,因为它已在很多地方使用过,但我想将此函数的响应返回给 react-native.如果有人知道这个问题,请告诉我.

for this function I cannot change the paremeter, becaused it had been used a lot of places, But I want to return that response from this function to react-native. If anybody know this issue, please let me know.

  @objc func sendEvent(_ response: APIResponse, callback: (NSObject) -> ()) 
-> Void {

    callback( [[
      "responseCode" : "Working",
      ]] as NSObject)

  }

我只想知道如何在 AckCallback 中使用这个 sendEvent,或者有没有其他方法可以发送 **

I just want to know how to use this sendEvent inside the AckCallback, or is there any other way to send that **

响应:API响应

**

反应原生.

推荐答案

对于第一个创建 Swift 类(例如 YourModule.swift)

For the first create Swift class (e.g YourModule.swift)

//
//  YourModule.swift
//

@objc(YourModule)
class YourModule: NSObject {

  @objc func callNativeEvent(callback:RCTResponseSenderBlock) 
-> Void {

   // Here you can do your work and pass an object to the callback function.
  // You can save assign a `callback` to the class property (e.g self.eventCallback = callback)
// and invoke that self.eventCallback after the asynchronous code ol somewhere else
  NSObject *obj = [[NSObject alloc] init]; // your object here
   callback([NSNull(), obj]);
   // or if you want to return an error
   // callback(["Error calling NativeEvent", NSNull()]);

  // I'm not sure that RCTResponseSenderBlock works the same as in previous react-native versions. Maybe now you can pass an Object instead of an Array.
  }
}

创建一个桥文件(例如YourModuleBridge.m)

Create a Bridge file (e.g. YourModuleBridge.m)

//
//  YourModuleBridge.m
//

#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"

#import <React/RCTBridgeModule.h>


@interface RCT_EXTERN_MODULE(YourModule, NSObject)

RCT_EXTERN_METHOD(callNativeEvent:(RCTResponseSenderBlock)callback);

@end

此外,如果您的项目中不存在 Bridging-Header 文件,则您需要该文件.

Also, you need Bridging-Header file if it doesn't exist in your project.

//
//  YourModule-Bridging-Header.h
//

#ifndef YourModule_Bridging_Header_h
#define YourModule_Bridging_Header_h

#if __has_include("RCTBridgeModule.h")
#import "RCTBridgeModule.h"
#else
#import <React/RCTBridgeModule.h>
#endif

#endif /* YourModule_Bridging_Header_h */

来自 JS

import { NativeModules } from 'react-native';

const YourModule = NativeModules.YourModule;

...

YourModule.callNativeEvent((error, response) => {
  console.log('Error', error, 'Response', response);
});

这篇关于如何从 React Native 调用 swift native 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆