React Native 31, New Appdelegate.m no localhost to change : Network Request Failed

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

I just upgraded to latest version (31) of react-native, my app which was working, is not working anymore. everytime i get Network Request Failed error from fetch.

AppDelegate.m is changed and there is no localhost or 127.0.0.1 to change.

is it placed somewhere else?

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

or should i change 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:

I found the solution. "Allow Arbitrary Loads" was removed from info.plist somehow. it should be added to info.plist and value should be "YES" otherwise app cannot access internet.

More info : React-Native fetch, Network request failed. not using localhost



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!