我正在尝试使用现有的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软件包时,会出现错误 No bundle URL present .
执行clean / npm安装/运行react-native run-ios没有帮助 . 直接在代码中设置URL有效,因此这不是防火墙/ AppTransportSecurity问题 .
我错过了什么?
AppDelegate.m
- RCTBundleURLProvider
返回的 jsCodeLocation
等于 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"
}
}
2 回答
不久前我在React Native上打开了an issue .
以下是一些解决方案:
运行
react-native run-ios
出现错误时,运行
npm install
然后再次运行
react-native run-ios
将
NSAllowsLocalNetworking
添加至info.plist
确保主机中有
127.0.0.1 localhost
杀死任何其他进程
sudo lsof -i :8081
,然后kill -9 <PID>
再次删除
YOUR_PROJECT/ios/build/
然后react-native run-ios
我希望其中一个解决方案能解决您的问题 . 如果没有,您可以在the issue找到更多选项 .
我今天遇到了这个“没有捆绑网址”的错误 . 这对我有用:
打开终端窗口
cd进入YOUR_PROJECT / ios
使用rm -r build删除 build 文件夹
再次运行react-native run-ios