我试图在一个开发的项目中创建一个 React-native“本机模块”(BankedSdk)M1
MacBook。 - 但是,任何初始化模块的尝试都会返回一个空对象。
这是适用于我的示例 (2020) 存储库intel
机器✅:https://github.com/banked/banked-react-native-sdk-example
在我更大的(基于 M1 的)项目中,我像这样集成了它:
在我的 iOS 文件夹中,我有三个新文件:
BankedSdk.h:
#import <Foundation/Foundation.h>
#import "React/RCTBridge.h"
NS_ASSUME_NONNULL_BEGIN
@interface BankedSdk : NSObject <RCTBridgeModule>
@end
NS_ASSUME_NONNULL_END
BankedSdk.m:
#import "BankedSdk.h"
#import "React/RCTLog.h"
#import "BankedReactNativeExample-Swift.h"
@implementation BankedSdk
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(initialise:(NSString *)apiKey)
{
[[BankedCheckoutWrapper shared] setUpWithApiKey: apiKey];
}
RCT_EXPORT_METHOD(openBankedSdk:(NSString *)paymentId and:(NSString *)continueUrl)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *presentedViewController = RCTPresentedViewController();
[[BankedCheckoutWrapper shared] presentCheckoutWithViewController: presentedViewController paymentId: paymentId continueURL: continueUrl];
});
}
RCT_EXPORT_METHOD(handlePaymentForURL:(NSURL *)url)
{
[[BankedCheckoutWrapper shared] handlePaymentWithUrl: url];
}
@end
BankedWrapper.swift:
import Foundation
import UIKit
import Banked
@objc class BankedCheckoutWrapper: NSObject {
@objc static let shared: BankedCheckoutWrapper = BankedCheckoutWrapper()
@objc func setUp(apiKey: String) {
BankedCheckout.shared.setUp(apiKey)
}
@objc func presentCheckout(viewController: UIViewController ,paymentId: String, continueURL: String) {
BankedCheckout.shared.presentCheckout(viewController , paymentId: paymentId, action: .pay, continueURL: continueURL) { response in
switch response {
case .success:
print("success")
case .failure(let error):
print("error \(error)")
}
}
}
@objc func handlePayment(url: URL) {
BankedCheckout.shared.handlePaymentWithURL(url, action: .pay) { response in
switch response {
case .success:
print("success")
case .failure(let error):
print("error \(error)")
}
}
}
}
然后我有一个名为Banked Sdk
内容如下:
import { NativeModules } from 'react-native'
const { BankedSdk } = NativeModules
export default BankedSdk
其调用方式如下:
import React, { useState, useEffect } from 'react'
[import a bunch of other stuff]
import BankedSdk from '@components/BankedSdk'
//Main file stuff
// -> TEST NATIVE MODULES BUTTON
function checkNativeModules() {
const apiKey = "xxxxxxxxxxxxxxxx"
BankedSdk.initialise(apiKey)
BankedSdk.openBankedSdk("xxxxxxxxxxxxxxxx", "urlHere")
console.log("Banked Sdk in Test Button: ", BankedSdk)
}
return (
<>
// Other UI stuff
<Button
title="Initiate Native Mod"
color="primary"
onPress={checkNativeModules}
/>
</>
)
}
export default Button
但是 - 我的 NativeModules 正在作为空对象注销:
Native Modules: {}
出现错误:
ERROR TypeError: null is not an object (evaluating '_BankedSdk.default.initialise')
(见图片)
***** 更新回复 @user3193920 *****
最终通过以下方法解决了这个问题:
在 Podfile 中:
# ENABLES BANKED (DYMANIC FRAMEWORK)
pod 'Banked', '0.0.25', :build_type => :dynamic_framework
BankedSdk.h:
#import <Foundation/Foundation.h>
#import "React/RCTBridge.h"
NS_ASSUME_NONNULL_BEGIN
@interface BankedSdk : NSObject <RCTBridgeModule>
@end
NS_ASSUME_NONNULL_END
BankedSdk.m:
#import "BankedSdk.h"
#import "React/RCTLog.h"
#import "systemSpendAppReactNativeOnM1-Swift.h"
@implementation BankedSdk
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(initialise:(NSString *)apiKey)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[BankedCheckoutWrapper shared] setUpWithApiKey: apiKey];
});
}
RCT_EXPORT_METHOD(openBankedSdk:(NSString *)paymentId and:(NSString *)continueUrl and:(RCTPromiseResolveBlock)resolve and:(RCTPromiseRejectBlock)reject)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *presentedViewController = RCTPresentedViewController();
[[BankedCheckoutWrapper shared] presentCheckoutWithViewController: presentedViewController paymentId: paymentId continueURL: continueUrl resolve: resolve rejecter: reject];
});
}
RCT_EXPORT_METHOD(handlePaymentForURL:(NSURL *)url)
{
dispatch_async(dispatch_get_main_queue(), ^{
[[BankedCheckoutWrapper shared] handlePaymentWithUrl: url];
});
}
@end
和 BankedWrapper.swift:
import Foundation
import UIKit
import Banked
@objc class BankedCheckoutWrapper: NSObject {
private var presentingCheckout = false {
didSet {
print("+++ presentingCheckout \(presentingCheckout.description)")
}
}
@objc static let shared: BankedCheckoutWrapper = BankedCheckoutWrapper()
@objc func setUp(apiKey: String) {
BankedCheckout.shared.setUp(apiKey)
}
@objc func presentCheckout(viewController: UIViewController ,paymentId: String, continueURL: String, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
presentingCheckout = true
return BankedCheckout.shared.presentCheckout(viewController , paymentId: paymentId, action: .pay, continueURL: continueURL) { [weak self] response in
guard let self = self else { return }
if self.presentingCheckout {
switch response {
case .success:
print("+++ success presentCheckout")
resolve("success")
case .failure(let error):
print("+++ error presentCheckout \(error)")
let errorNew = NSError(domain: "", code: 200, userInfo: nil)
reject("Caught error", "error", errorNew)
}
}
self.presentingCheckout = false
}
}
@objc func handlePayment(url: URL) {
BankedCheckout.shared.handlePaymentWithURL(url, action: .pay) { response in
switch response {
case .success:
print("+++ success handlePayment")
case .failure(let error):
print("+++ error handlePayment \(error)")
}
}
}
}