我可以在不使用Objective-C(纯Swift)的情况下向React Native公开Swift函数吗



我想知道是否可以将我的func navigateToLoginWidget公开给React Native。以便可以从RN触发它。

我已经成功地将React Native附带的Objective-C模板更改为Swift,如下所示:

import Foundation
import IBMCloudAppID
import BMSCore
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
var bridge: RCTBridge!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

// initializing App ID SDK
let region = AppID.REGION_SOMEWHERE
let backendGUID = "MY_TENANT_ID_FROM_IBM"
AppID.sharedInstance.initialize(tenantId: backendGUID, region: region)

// Initializing React Native front-end with Swift instead of Obj-c template
let jsCodeLocation: URL
jsCodeLocation = RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: "index.js", fallbackResource:nil)
let rootView = RCTRootView(bundleURL: jsCodeLocation, moduleName: "MY_PROJECT_NAME", initialProperties: nil, launchOptions: launchOptions)
let rootViewController = UIViewController()
rootViewController.view = rootView
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = rootViewController
self.window?.makeKeyAndVisible()
return true
}

func application(_ application: UIApplication, open url: URL, options :[UIApplication.OpenURLOptionsKey : Any]) -> Bool {
return AppID.sharedInstance.application(application, open: url, options: options)
}

func navigateToLoginWidget(_ sender : Any) {
print("clicked")
AppID.sharedInstance.loginWidget?.launch(delegate: SigninDelegate)
}

}

我通常会在另一个名为SigninDelegate.swift的模块中拥有这个函数,但出于解释的目的,我将它包含在同一个类中。

根据官方文件:

Swift不支持宏,因此将其公开给React Native需要更多的设置,但工作原理相对相同。

所以是的,这是可能的,但需要一些设置。查看上面链接中的文档,了解设置它所需的步骤。

最新更新