javascript - React Native 31,New Appdelegate.m 没有要更改的本地主机 : Network Request Failed

标签 javascript ios reactjs react-native

我刚刚升级到 react-native 的最新版本 (31),我的应用程序运行正常,但不再运行了。每次我从 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;
  }

最佳答案

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

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

关于javascript - React Native 31,New Appdelegate.m 没有要更改的本地主机 : Network Request Failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38971628/

相关文章:

ios - iOS 5 下的 Haskell : suppressing output

ios - 当应用程序进入后台时无法设置亮度,任何人都可以对此有任何想法吗?

javascript - 在 React 中操作 DOM

javascript - Firebase自定义身份验证: Security of Firebase Secret in JavaScript

javascript - 当存在具有特定名称的键时,数组将对象推送到数组中

javascript - Angularjs 代码不工作

jquery - 对于这个复杂的例子,如何 "think in React"?

javascript - 谷歌地图API自动完成显示位置的 map 预览

ios - 如何让所有 CFTree Siblings 处于相同的深度?

javascript - useRef 对象在 React/React-Native 中返回未定义