React Native 31,新的 Appdelegate.m 没有要更改的本地主机:网络请求失败

Posted

技术标签:

【中文标题】React Native 31,新的 Appdelegate.m 没有要更改的本地主机:网络请求失败【英文标题】:React Native 31, New Appdelegate.m no localhost to change : Network Request Failed 【发布时间】:2016-08-16 09:47:07 【问题描述】:

我刚刚升级到最新版本 (31) 的 react-native,我的应用程序正在运行,但不再运行了。每次我从 fetch 中得到 Network Request Failed 错误。

AppDelegate.m 已更改,没有 localhost 或 127.0.0.1 可更改。

它是放在别的地方吗?

AppDelegate.m:

#import "AppDelegate.h"

#import "RCTBundleURLProvider.h"
#import "RCTRootView.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

  NSURL *jsCodeLocation;

  jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

  RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:jsCodeLocation
                                                      moduleName:@"OpenCampus"
                                               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;


@end

或者我应该更改 RCTWebSocketExecutor.m 吗?

if (!_url) 
    NSUserDefaults *standardDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger port = [standardDefaults integerForKey:@"websocket-executor-port"] ?: 8081;
    NSString *host = [[_bridge bundleURL] host];
    if (!host) 
      host = @"localhost";
    
    NSString *URLString = [NSString stringWithFormat:@"http://%@:%zd/debugger-proxy?role=client", host, port];
    _url = [RCTConvert NSURL:URLString];
  

  _jsQueue = dispatch_queue_create("com.facebook.react.WebSocketExecutor", DISPATCH_QUEUE_SERIAL);
  _socket = [[RCTSRWebSocket alloc] initWithURL:_url];
  _socket.delegate = self;
  _callbacks = [NSMutableDictionary new];
  _injectedObjects = [NSMutableDictionary new];
  [_socket setDelegateDispatchQueue:_jsQueue];

  NSURL *startDevToolsURL = [NSURL URLWithString:@"/launch-js-devtools" relativeToURL:_url];
  [NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:startDevToolsURL] delegate:nil];

  if (![self connectToProxy]) 
    RCTLogError(@"Connection to %@ timed out. Are you running node proxy? If "
                 "you are running on the device, check if you have the right IP "
                 "address in `RCTWebSocketExecutor.m`.", _url);
    [self invalidate];
    return;
  

【问题讨论】:

【参考方案1】:

我找到了解决方案。 “允许任意负载”以某种方式从 info.plist 中删除。它应该被添加到 info.plist 并且值应该是“YES”否则应用程序无法访问互联网。

更多信息:React-Native fetch, Network request failed. not using localhost

【讨论】:

以上是关于React Native 31,新的 Appdelegate.m 没有要更改的本地主机:网络请求失败的主要内容,如果未能解决你的问题,请参考以下文章

React-Native:无法将 babel 更新到 ES7

react-native Navigator 和非堆叠导航(如树)

在 App Store 中初始化新的 react native 项目并替换为当前的 react native 项目

“flux”和“redux”与“react native”有啥关系?新的“react native”应该开始使用其中的哪一个?

react-native 0.31.0 低版本在Android上报网络的错误错

使用 react-native-push-notification,App 在收到新的 FCM 通知时崩溃