Running react-native app on iOS device using offline bundle

前端 未结 6 1171
广开言路
广开言路 2020-12-14 09:37

The official React Native documentation to run app on iOS device using offline bundle says

Open ios/YourApp/AppDelegate.m

Uncomment the line, jsCo

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 10:12

    The only fix I could get to work on react native 0.37 was to go back to the "old school" method of manually editing jsCodeLocation in AppDelegate.m. Debugging is good again!

    Be sure your device and development machine are on the same wi-fi, and maybe disconnect any wired Ethernet until you get it working.

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
      NSURL *jsCodeLocation;
    
      // Comment-out, cannot get past error: bundleURL must be non-nil when not implementing loadSourceForBridge
      //jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];
    
    #if DEBUG
      NSLog(@"AppDelegate:DEBUG");
    
    #if TARGET_IPHONE_SIMULATOR
      NSLog(@"AppDelegate:DEBUG:TARGET_IPHONE_SIMULATOR");
      jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle"];
    #else
      NSLog(@"AppDelegate:DEBUG:!TARGET_IPHONE_SIMULATOR");
      NSLog(@"To device debug, open RCTWebSocketExecutor.m & replace localhost with MacBook IP.");
      // Get dev host IP Address:
      //    ifconfig | grep inet\ | tail -1 | cut -d " " -f 2
      jsCodeLocation = [NSURL URLWithString:@"http://192.16.29.213:8081/index.ios.bundle"];
    #endif
    
    #else
      NSLog(@"AppDelegate:RELEASE jsbundle");
      jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
    #endif
    
      NSLog(@"jsCodeLocation = %@",jsCodeLocation);
    
    
      RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                          moduleName:@"BluetoothConnect"
                                                   initialProperties:nil
                                                       launchOptions:launchOptions];
      rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
    
      self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
      UIViewController *rootViewController = [UIViewController new];
      rootViewController.view = rootView;
      self.window.rootViewController = rootViewController;
      [self.window makeKeyAndVisible];
      return YES;
    }
    

提交回复
热议问题