React Native 31,New Appdelegate.m没有要更改的localhost:网络请求失败

时间:2016-08-16 09:47:07

标签: javascript ios reactjs react-native

我刚刚升级到本机的最新版本(31),我的应用程序正在运行,不再工作了。每次我从获取中获得网络请求失败错误。

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 个答案:

答案 0 :(得分:0)

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

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