自定义React Native项目中不存在捆绑包URL

时间:2017-12-07 13:37:05

标签: ios objective-c react-native

我正在尝试使现有的iOS应用程序使用React Native,因此我编写了代码,用于手动桥接两个,而不是使用提供的CLI工具。我希望RCTBundleURLProvider自动检测捆绑服务器的IP地址,就像在CLI工具生成的应用程序中一样。

最后,当我运行应用程序时:

react-native run-ios
react-native start --> the bundle server doesn't start automatically, which is strange

在模拟器或设备中加载React Native捆绑包时,会出现错误无捆绑网址

执行clean / npm安装/运行react-native run-ios没有帮助。直接在代码中设置URL有效,因此这不是防火墙/ AppTransportSecurity问题。

我错过了什么?

AppDelegate.m返回的

jsCodeLocation - RCTBundleURLProvider等于null

...
- (void) loadReact:(NSDictionary*) launchOptions {

    NSURL *jsCodeLocation;

    jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
    // this WORKS --> jsCodeLocation = [NSURL URLWithString:@"http://xxx.xxx.xxx.xxx:8081/index.bundle?platform=ios"];

    NSLog(@"JSCodeLocation:%@",jsCodeLocation); // jsCodeLocation equals null 

    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                        moduleName:@"MyModule"
                                                 initialProperties:nil
                                                     launchOptions:launchOptions];
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view = rootView;

    rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = vc;
    [self.window makeKeyAndVisible];
}
...

RCTBundleURLProvider.m查找名为ip.txt的文件,该文件似乎缺失。

...
- (NSString *)guessPackagerHost
{
  static NSString *ipGuess;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    NSString *ipPath = [[NSBundle mainBundle] pathForResource:@"ip" ofType:@"txt"];
    ipGuess = [[NSString stringWithContentsOfFile:ipPath encoding:NSUTF8StringEncoding error:nil]
               stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
  });

  NSString *host = ipGuess ?: @"localhost";
  if ([self isPackagerRunning:host]) {
    return host;
  }
  return nil;
}
...

package.json

{
  "name": "MyModule",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "react": "16.0.0",
    "react-native": "0.51.0"
  },
  "devDependencies": {
    "babel-jest": "21.2.0",
    "babel-preset-react-native": "4.0.0",
    "jest": "21.2.1",
    "react-test-renderer": "16.0.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

3 个答案:

答案 0 :(得分:2)

我不久前在React Native上打开了an issue

以下是一些解决方案:

    • 运行react-native run-ios
    • 出现错误时,请运行npm install
    • 然后再次运行react-native run-ios
  1. NSAllowsLocalNetworking添加到info.plist

  2. 确保主机中有127.0.0.1 localhost

  3. 杀死任何其他进程sudo lsof -i :8081,然后kill -9 <PID>

  4. 再次移除YOUR_PROJECT/ios/build/,然后移除react-native run-ios

  5. 我希望其中一种解决方案可以解决您的问题。如果没有,您可以在the issue上找到更多选项。

答案 1 :(得分:0)

我今天遇到了此“无捆绑包网址存在”错误。这对我有用:

  • 打开终端窗口
  • 光盘插入YOUR_PROJECT / ios
  • 使用rm -r build删除 build 文件夹
  • 再次运行react-native run-ios

答案 2 :(得分:0)

运行以下代码:

killall -9 node
rm -rf ios/build
react-native run-ios

它将打开launchpackager.command并将应用程序安装在ios模拟器上