swift – Unable to get JSI runtime in React Native iOS bridge


Drawback Description

I am making an attempt to implement a JSI Bridge in my React Native iOS app, however I can not get the runtime object from RCTCxxBridge after the RCTJavaScriptDidLoadNotification is triggered (it prints as nil).

Code Implementation

JSIBridge.h

#import 
#import 

NS_ASSUME_NONNULL_BEGIN

@interface JSIBridge : NSObject

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

@finish

NS_ASSUME_NONNULL_END

JSIBridge.mm

#import "JSIBridge.h"
#embody 
#import 

utilizing namespace fb::jsi;
utilizing namespace std;

@implementation JSIBridge

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

@finish

AppDelegate.swift

@predominant
class AppDelegate: UIResponder, UIApplicationDelegate {

  var window: UIWindow?

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

ReactNativeFactoryManager.swift

import React
import React_RCTAppDelegate

@objc class ReactNativeFactoryManager: NSObject {
  // Singleton occasion
  @objc static let shared = ReactNativeFactoryManager()
  
  non-public let reactNativeFactory: RCTReactNativeFactory
  non-public let delegate: ReactNativeDelegate
  
  // Non-public initializer to make sure singleton utilization
  non-public override init() {
    delegate = ReactNativeDelegate()
    reactNativeFactory = RCTReactNativeFactory(delegate: delegate)
    tremendous.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 accurately.
  2. I can get the ⁠RCTCxxBridge object, however its runtime ⁠property is nil
  3. I’ve tried getting the bridge object each from ⁠notification.userInfo[@"bridge"] and ⁠ReactNativeFactoryManager.shared.getFactory().bridge

Surroundings

  • React Native model: 0.78
  • iOS model: 18.2
  • Xcode model: 16.3
  • Machine/Simulator: iOS Simulator

Questions

  1. Why cannot I get the ⁠runtime object?
  2. Ought to the ⁠runtime be initialized by the point the ⁠RCTJavaScriptDidLoadNotification is triggered?
  3. Is there one other method or timing to accurately get the JSI ⁠runtime?

I believe there may be adjustments in how JSI bridging is initialized or accessed in React Native 0.78. If anybody has encountered related points or is aware of the right approach to entry the JSI runtime within the newest React Native variations, I might significantly admire your assist.
Thanks for any help or ideas!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles