最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Unable to get JSI runtime in React Native iOS bridge - Stack Overflow

programmeradmin2浏览0评论

Problem Description

I'm trying to implement a JSI Bridge in my React Native iOS app, but I can't get the runtime object from RCTCxxBridge after the RCTJavaScriptDidLoadNotification is triggered (it prints as nil).

Code Implementation

JSIBridge.h

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

NS_ASSUME_NONNULL_BEGIN

@interface JSIBridge : NSObject

+ (void)setupBridge:(NSNotification *)notification bridge:(nullable RCTBridge *)bridge;

@end

NS_ASSUME_NONNULL_END

JSIBridge.mm

#import "JSIBridge.h"
#include <jsi/jsi.h>
#import <React/RCTBridge+Private.h>

using namespace facebook::jsi;
using namespace std;

@implementation JSIBridge

+ (void)setupBridge:(NSNotification *)notification bridge:(RCTBridge *)bridge {
  RCTCxxBridge* cxxbridge = (RCTCxxBridge*)notification.userInfo[@"bridge"];
  NSLog(@"[JSIBridge]: %@", cxxbridge.runtime);
}

@end

AppDelegate.swift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    ReactNativeFactoryManager.shared.setupBridge(launchOptions: launchOptions)
    
    NotificationCenter.default.addObserver(self,
                                           selector: #selector(onJSLoad(notification:)),
                                           name: NSNotification.Name("RCTJavaScriptDidLoadNotification"),
                                           object: nil)
    
    return true
  }
  
  @objc func onJSLoad(notification: Notification) {
    JSIBridge.setupBridge(notification, bridge: ReactNativeFactoryManager.shared.getFactory().bridge)
  }
  
  // Other code...
}

ReactNativeFactoryManager.swift

import React
import React_RCTAppDelegate

@objc class ReactNativeFactoryManager: NSObject {
  // Singleton instance
  @objc static let shared = ReactNativeFactoryManager()
  
  private let reactNativeFactory: RCTReactNativeFactory
  private let delegate: ReactNativeDelegate
  
  // Private initializer to ensure singleton usage
  private override init() {
    delegate = ReactNativeDelegate()
    reactNativeFactory = RCTReactNativeFactory(delegate: delegate)
    super.init()
  }
  
  @objc func getFactory() -> RCTReactNativeFactory {
    return reactNativeFactory
  }
  
  func setupBridge(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
    reactNativeFactory.bridge = delegate.createBridge(with: delegate, launchOptions: launchOptions ?? [:])
  }
}

What I've Tried

  1. I've confirmed that the ⁠RCTJavaScriptDidLoadNotification notification is triggered correctly.
  2. I can get the ⁠RCTCxxBridge object, but its runtime ⁠property is nil
  3. I've tried getting the bridge object both from ⁠notification.userInfo[@"bridge"] and ⁠ReactNativeFactoryManager.shared.getFactory().bridge

Environment

  • React Native version: 0.78
  • iOS version: 18.2
  • Xcode version: 16.3
  • Device/Simulator: iOS Simulator

Questions

  1. Why can't I get the ⁠runtime object?
  2. Should the ⁠runtime be initialized by the time the ⁠RCTJavaScriptDidLoadNotification is triggered?
  3. Is there another way or timing to correctly get the JSI ⁠runtime?

I suspect there might be changes in how JSI bridging is initialized or accessed in React Native 0.78. If anyone has encountered similar issues or knows the correct way to access the JSI runtime in the latest React Native versions, I would greatly appreciate your help. Thank you for any assistance or suggestions!

发布评论

评论列表(0)

  1. 暂无评论