如何将 React Native Promise 桥接到 Swift [英] How to bridge React Native Promise to Swift

查看:97
本文介绍了如何将 React Native Promise 桥接到 Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位软件爱好者,

我目前正在开发一个 React 原生项目,我需要为其添加一些以 swift 编写的逻辑.我能够通过连接到 Objective C 和 Swift 来触发一个基本的 swift 函数.

I am currently working on a React native project for which I need to add some logic which has been written in swift. I am able to trigger a basic swift function through the bridging to Objective C an then to Swift.

当我尝试用 Promise 做某事时会出现问题.我描述这一点的页面在 Promises 的 Objective C 部分以及与 Swift 的桥接上很清楚,但对于 swift 的承诺则不然:https://facebook.github.io/react-native/docs/native-modules-ios.html

The problem occurs when I try to do something with promises. The page I describing this is clear on the Objective C part for Promises and also for bridging to Swift, but not so on promises to swift: https://facebook.github.io/react-native/docs/native-modules-ios.html

这就是我所拥有的:

Project-Bridging-Header.h

#import <React/RCTBridgeModule.h>

MyLoginBridge.m

#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>

@interface RCT_EXTERN_REMAP_MODULE(MyCustomLoginJSName, MyLoginModule, NSObject)

RCT_EXTERN_REMAP_METHOD(loginWithEmail,
                    resolver:(RCTPromiseResolveBlock)resolve
                    rejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(testMethod)

@end

MyLoginModule.swift

import Foundation

@objc(TripleASDKModule)
class TripleASDKModule: NSObject {

  @objc
  func loginWithEmail(resolver resolve: RCTPromiseResolveBlock,  rejecter reject: RCTPromiseRejectBlock) -> Void {
    resolve("This method is troublesome")
  }

  @objc func testMethod() -> Void {
    print("This Does appear")
  }
}

当我触发 testMethod 时,打印会显示在 Xcode 中,以便执行 swift 代码.但是当我调用 loginWithEmail 方法时,我看到臭名昭著的红色 React Native 错误屏幕说:

When i trigger the testMethod, the print is shown in Xcode, so that swift code is executed. But when I call the loginWithEmail method, I get the infamous red React Native error screen saying:

Exception 'resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject is not a recognized Objective-C method.' was thrown while invoking loginWithEmail on target MyCustomLoginJSName with params (
    30,
    31
)

为了完整起见,Javascript 端:

And for the sake of completeness, the Javascript side:

const loginWithEmail = () => NativeModules.TripleA.loginWithEmail()
    .then(result => console.log(result));

我尝试了几乎所有 RCT_EXTERN_REMAP_METHOD 等我能找到的变体,无论有没有重新映射重复名称等.因此,如果这个问题听起来很熟悉,或者您可以指导我朝着正确的方向前进,请这样做,感谢您的帮助.

I tried almost all variations of RCT_EXTERN_REMAP_METHOD and the like I could find, both with and without Remapping repeating the name, etc. So if this problem sound familiar, or you could guide me in the right direction, please do so, any help is appreciated.

推荐答案

摘自 Got 的答案-C 方法"将 Swift 桥接到 React-Native 时;它不起作用的事实是因为第一个参数标签的不同.

Taken from the answers at Got "is not a recognized Objective-C method" when bridging Swift to React-Native; the fact that it doesn't work, is because of the difference in the first argument labels.

为了让它与您的初始代码一起工作,您应该将 Swift 的第一个参数编写为没有名称,如下所示:

To make it work with your initial code you should write the first argument of your Swift to be without a name, like this:

@objc
func loginWithEmail(_ resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) -> Void {
    //   the trick  ^
    resolve("This method is no longer troublesome")
}

这篇关于如何将 React Native Promise 桥接到 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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