RCTRootViewマルチエントリ初期化ホワイトスクリーン問題

5282 ワード

RCTRootViewソースコードを表示すると、2つの初期化方法が表示されます.複数のRCTRootView初期化、すなわち複数のエントリがある場合、RNは''initWithBridge:moduleName:'を初期化方法として使用することを推奨します.
/**
 * - Designated initializer -
 */
- (instancetype)initWithBridge:(RCTBridge *)bridge
                    moduleName:(NSString *)moduleName
             initialProperties:(NSDictionary *)initialProperties NS_DESIGNATED_INITIALIZER;

/**
 * - Convenience initializer -
 * A bridge will be created internally.
 * This initializer is intended to be used when the app has a single RCTRootView,
 * otherwise create an `RCTBridge` and pass it in via `initWithBridge:moduleName:`
 * to all the instances.
 */
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
                       moduleName:(NSString *)moduleName
                initialProperties:(NSDictionary *)initialProperties
                    launchOptions:(NSDictionary *)launchOptions;

/**
 * The name of the JavaScript module to execute within the
 * specified scriptURL (required). Setting this will not have
 * any immediate effect, but it must be done prior to loading
 * the script.
 */

通常、RNプログラムの単一のエントリがマルチエントリを紹介していない場合、RCTbridgeオブジェクトを初期化してRCTbridgeソースコードの発見を表示する必要があります.
/**
 * Creates a new bridge with a custom RCTBridgeDelegate.
 *
 * All the interaction with the JavaScript context should be done using the bridge
 * instance of the RCTBridgeModules. Modules will be automatically instantiated
 * using the default contructor, but you can optionally pass in an array of
 * pre-initialized module instances if they require additional init parameters
 * or configuration.
 */
- (instancetype)initWithDelegate:(id)delegate
                   launchOptions:(NSDictionary *)launchOptions;

/**
 * DEPRECATED: Use initWithDelegate:launchOptions: instead
 *
 * The designated initializer. This creates a new bridge on top of the specified
 * executor. The bridge should then be used for all subsequent communication
 * with the JavaScript code running in the executor. Modules will be automatically
 * instantiated using the default contructor, but you can optionally pass in an
 * array of pre-initialized module instances if they require additional init
 * parameters or configuration.
 */
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
                   moduleProvider:(RCTBridgeModuleProviderBlock)block
                    launchOptions:(NSDictionary *)launchOptions;

/**


2番目の初期化方法はすでに廃棄されています.
使用方法RN中国語網は以下の解釈を与える.http://reactnative.cn/docs/0.47/native-modules-ios.html
id moduleInitialiser = [[classThatImplementsRCTBridgeDelegate alloc] init];

RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:moduleInitialiser launchOptions:nil];

RCTRootView *rootView = [[RCTRootView alloc]
                        initWithBridge:bridge
                            moduleName:kModuleName
                     initialProperties:nil];
id moduleInitialiser = [[classThatImplementsRCTBridgeDelegate alloc] init];

このclassThatImplementsRCTbridgeDelegateはよくわかりませんが、ここではRCTbridgeDelegateプロトコルを実装するクラスです.RCTbridgeDelegateソースコードを参照してください.

@protocol RCTBridgeDelegate 

/**
 * The location of the JavaScript source file. When running from the packager
 * this should be an absolute URL, e.g. `http://localhost:8081/index.ios.bundle`.
 * When running from a locally bundled JS file, this should be a `file://` url
 * pointing to a path inside the app resources, e.g. `file://.../main.jsbundle`.
 */
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge;

sourceURLForBridgeメソッドがあります
では、RCTbridgeDelegateエージェントメソッドに従ってクラスを新規作成し、sourceURLForBridgeメソッドを実装します.
#import 
#import 
#import 

@interface MallBridgeAPI : RCTBridge
/**    */
+ (MallBridgeAPI *)shareInstance;
@end

@interface MallBridgeHandle : NSObject

@end

#import "MallBridgeAPI.h"


@implementation MallBridgeAPI
static MallBridgeAPI *defaultHandle = nil;
+ (MallBridgeAPI *)shareInstance
{
    @synchronized(self){
        if (defaultHandle == nil) {
            defaultHandle = [[MallBridgeAPI alloc] initWithDelegate:[[MallBridgeHandle alloc]init] launchOptions:nil];
        }
    }
    return defaultHandle;
}
@end


@implementation MallBridgeHandle

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
    return [NSURL URLWithString:@"http://127.0.0.1:8081/index.ios.bundle?platform=ios"];
#else
    return [[NSBundle mainBundle] URLForResource:@"index.ios" withExtension:@"jsbundle"];
#endif
    
}
@end

ここでRCTbridgeインスタンスを使用すると、classThatImplementsRCTbridgeDelegateを使用して初期化を完了できます.
  
self.rootView = [[RCTRootView alloc]initWithBridge:[MallBridgeAPI shareInstance]
                                          moduleName:@"MyShopVC"
                                   initialProperties:@{@"schemeType":@"debug",@"isSelectedProvider":@(isSelectedProvider)}];
  
}