Brownfield usage of Fabric with Swift #143
-
I am trying to activate Fabric within an existing iOS app to use Turbo Native Modules. The app was created using Swift, but there are no examples of changes that must be made to use Fabric when using Swift. What I've tried so far:
After that I ended up with this import UIKit
import React
import RCTFabric
@main
class AppDelegate: UIResponder, UIApplicationDelegate, RCTBridgeDelegate {
func sourceURL(for bridge: RCTBridge!) -> URL! {
#if DEBUG
return URL(string: "http://localhost:8081/index.bundle?platform=ios")
#else
return Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
}
static var bridge: RCTBridge?
#if RN_FABRIC_ENABLED
static var bridgeAdapter: RCTSurfacePresenterBridgeAdapter?
#endif
var window: UIWindow?
#if RN_FABRIC_ENABLED
var bridgeAdapter: RCTSurfacePresenterBridgeAdapter?
var reactNativeConfig: Unmanaged<ReactNativeConfig>?
var contextContainer: Unmanaged<ContextContainer>?
#endif
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.//
#if RN_FABRIC_ENABLED
contextContainer = Unmanaged.passRetained(ContextContainer())
reactNativeConfig = Unmanaged.passRetained(EmptyReactNativeConfig())
contextContainer?.takeUnretainedValue().insert("ReactNativeConfig", reactNativeConfig?.takeUnretainedValue())
bridgeAdapter = RCTSurfacePresenterBridgeAdapter(bridge: bridge, contextContainer: contextContainer?.takeUnretainedValue())
AppDelegate.bridge.surfacePresenter = bridgeAdapter?.surfacePresenter
#endif
return true
}
...
} If I try to build the project after running
If any more information is needed, I am happy to provide it. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
Hi @flokol120! Unfortunately, Fabric cannot be used with Swift yet. You can create TurboModules in Swift, adding a thin wrapper of ObjectiveC++ though. We have prepared examples here. Look at the table in the README for more information. |
Beta Was this translation helpful? Give feedback.
-
Take a look at https://github.com/microsoft/react-native-test-app , at least the macOS setup uses Swift for the AppDelegate and therefore probably also iOS, I just haven't tested it with iOS. But they do quite some interesting stuff there! |
Beta Was this translation helpful? Give feedback.
Hi @flokol120! Unfortunately, Fabric cannot be used with Swift yet.
There are some architectural changes we need to apply to our C++ layer in order to make it work properly with Swift.
You can create TurboModules in Swift, adding a thin wrapper of ObjectiveC++ though. We have prepared examples here. Look at the table in the README for more information.