首页 > 解决方案 > 如果有两个条件,如何结合jsbundle和代码推送?

问题描述

我在 XCode 上的 React Native 项目中设置了代码推送。

  #ifdef DEBUG
    jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  #else
    jsCodeLocation = [CodePush bundleURL];
  #endif

现在我想生成 jsbundle 并在 IOS 设备上构建项目。

但我发现这jsCodeLocation是重复的。

#ifdef DEBUG
    jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
#else
    jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif

我很困惑如何结合 jsCodeLocation ?

我不熟悉Objective-c。

标签: objective-cxcodereact-native

解决方案


它在示例中提供:

#import "AppDelegate.h"

#import "RCTRootView.h"

#import "CodePush.h"

#import "RCTLog.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  RCTSetLogThreshold(RCTLogLevelInfo);

  NSURL *jsCodeLocation;

  jsCodeLocation = [CodePush bundleURL];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"CODE_PUSH_TEST_APP_NAME"
                                               initialProperties:nil
                                                   launchOptions:launchOptions];

  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
  UIViewController *rootViewController = [[UIViewController alloc] init];
  rootViewController.view = rootView;
  self.window.rootViewController = rootViewController;
  [self.window makeKeyAndVisible];
  return YES;
}

@end

https://github.com/Microsoft/react-native-code-push/blob/master/test/template/ios/TestCodePush/AppDelegate.m


推荐阅读