How to manually link the lib in the iOS example app #506
-
I need to inject some dependencies into the iOS lib, like This is what I tried to manually link the lib inside example/ios/Podfile pod 'MyLibName', :path => '../..' # pointing to the root podspec of the lib Disabled automatic linking for this lib const path = require('path');
const pak = require('../package.json');
module.exports = {
dependencies: {
[pak.name]: {
root: path.join(__dirname, '..'),
platforms: {
android: null,
ios: null,
// add more platform to disable auto-linking for them too
},
},
},
}; Create the RNModuleInitializer and call it in the AppDelegate // RNModuleInitializer.swift
import Foundation
import React
import MyLibName
@objc(RNModuleInitializer)
final class RNModuleInitializer: NSObject {}
extension RNModuleInitializer: RCTBridgeDelegate {
func sourceURL(for bridge: RCTBridge!) -> URL! {
return URL(string: "http://localhost:8081/index.ios.bundle?platform=ios")!
}
func extraModules(for bridge: RCTBridge!) -> [RCTBridgeModule]! {
var extraModules = [RCTBridgeModule]()
extraModules.append(
MyLibName("myParam1") // HERE Is the problem. Cannot call value of non-function type 'module<MyLibName>'
)
return extraModules
}
} Instantiate the RNModuleInitialiser in AppDelgate that holds the extraModules - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleName = moduleName;
// Begin - Initialize Native modules manually inside RNModuleInitialiser
// https://reactnative.dev/docs/native-modules-ios.html#dependency-injection
// https://stackoverflow.com/questions/37142310/dependency-injection-in-react-native-modules
// Initialise a class that implements RCTBridgeDelegate
// Be warned, RCTBridge won't store a strong reference to your delegate.
// You should there store this delegate as a property of your initialising class, or implement the protocol in the View Controller itself.
self.moduleInitializer = [[RNModuleInitializer alloc] init];
// id<RCTBridgeDelegate> moduleInitializer = [[RNModuleInitializer alloc] init];
// Create a RCTBridge instance
// (you may store this in a static context if you wish to use with other RCTViews you might initialise.
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self.moduleInitializer launchOptions:nil];
// Initialise an RCTRootView
RCTRootView *rootView = [[RCTRootView alloc]
initWithBridge:bridge
moduleName:moduleName
initialProperties:nil];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// End - Initialize Native modules manually inside RNModuleInitialiser
// You can add your custom initial props in the dictionary below.
// They will be passed down to the ViewController used by React Native.
self.initialProps = @{};
return YES;
} But when I instantiate the native module, this I'm getting a compile error |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is an example I used of configuring an external native module generated by react-native-bob-builder and allowing the injection of dependencies to it. The React-native docs seem incomplete for manual linking for iOS, so I didn't actually follow it 100%, instead, I created a solution based on some other repos I found.
module.exports = {
dependencies: {
['myLibJSPackageName']: {
platforms: {
android: null,
ios: null, // Disable ios linking
// Add more platforms to disable auto-linking for them too
},
},
},
};
target 'MyLibExample' do
pod 'myLibPodSpecName', :path => '../node_modules/myLibJSPackageName' // If this is inside the example app generated by react-native-bob-builder, the path would be :path => '../..' instead, pointing to the podspec
...
end
#import <React/RCTBridgeModule.h>
#import <React/RCTEventEmitter.h>
@interface MyLibName : RCTEventEmitter <RCTBridgeModule>
- (instancetype)initWithParam1:(NSString *)param1 param2:(NSString *)param2;
@end
#import <myLibPodSpecName/MyLibName.h>
...
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.moduleName = moduleName;
// Start - Initialize bridge manually
// Create a RCTBridge instance with the extraModulesForBridge delegate being the AppDelegate itself
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:nil];
RCTRootView *rootView = [[RCTRootView alloc]
initWithBridge:bridge
moduleName:moduleName
initialProperties:nil];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
// End - Initialize bridge manually
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
// Add this, if does not exists yet.
- (NSArray<id<RCTBridgeModule>> *)extraModulesForBridge:(RCTBridge *)bridge
{
NSMutableArray<id<RCTBridgeModule>> *extraModules = [NSMutableArray array];
// Here the lib is initialized with custom params
MyLibName *instance = [[MyLibName alloc] initWithParam1:@"value1" param2:@"value2"];
[extraModules addObject:instance];
return extraModules;
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
...
@end |
Beta Was this translation helpful? Give feedback.
This is an example I used of configuring an external native module generated by react-native-bob-builder and allowing the injection of dependencies to it.
The React-native docs seem incomplete for manual linking for iOS, so I didn't actually follow it 100%, instead, I created a solution based on some other repos I found.
ta…