简体   繁体   English

真正的IOS设备不会发出网络请求-React Native

[英]Real IOS Device will not make network requests - React Native

I am attempting to get my React Native application working on a real iPhone so I can begin integrating the app with branch.io. 我试图让我的React Native应用程序在真实的iPhone上运行,以便我可以开始将该应用程序与branch.io集成。 For some reason I cannot get my app to make network requests to my backend api on AWS. 由于某些原因,我无法让我的应用向我的AWS后端api发出网络请求。 My code works as expected in the simulator when I hit the backend api on my local dev environment with localhost:3000/auth/login/. 当我使用localhost:3000 / auth / login /在本地开发环境中访问后端api时,我的代码可以在模拟器中按预期工作。 Below are the results of my efforts. 以下是我努力的结果。

HTTP Class Post Method: HTTP类发布方法:

post(endpoint, body, auth = null) {

  console.log(this.apiUrl + endpoint);
  console.log(this._headers(true));
  console.log(body);

  return Observable.defer(() => {
    return Observable.fromPromise(fetch(this.apiUrl + endpoint, {
      method: 'post',
      headers: this._headers(auth),
      body: JSON.stringify(body)
    })
    .then(this._checkStatus)
    .then(res => res.json()));
  });
}

_headers(auth) {
  let token = (auth) ? token = Config.clientId : this.accessToken;
  let headers = new Headers({
    'Content-Type': 'application/json',
    'Authorization': 'Bearer ' + token
  });

  return headers;
}

After building the code to my iOS device I attempt to login and nothing happens. 将代码构建到我的iOS设备后,我尝试登录,但没有任何反应。 I added console calls to my post request to make sure the proper values are being passed and that seems to be correct. 我在我的发布请求中添加了控制台调用,以确保传递正确的值,这似乎是正确的。 Below is the results of what I see when I attempt to login while running debug mode. 以下是运行调试模式时尝试登录时看到的结果。 I am new to React Native so I am not sure if the network tab would detect requests coming from a real device, nonetheless there is no sign of any network call being made. 我是React Native的新手,因此不确定网络选项卡是否会检测到来自真实设备的请求,尽管如此,没有任何进行网络调用的迹象。

在此处输入图片说明

Running the same exact call from Postman works as expected and can be seen below: 从邮递员运行相同的确切呼叫可以按预期工作,如下所示:

在此处输入图片说明

Any assistance would be greatly appreciated. 任何帮助将不胜感激。 Thanks you in advance. 预先谢谢你。

Because you are likely using HTTP and not HTTPS, you need to enable NSAppTransportSecurity and add your domain to the NSExceptionDomains list in your info.plist. 由于您可能使用的是HTTP,而不是HTTPS,因此需要启用NSAppTransportSecurity并将域添加到info.plist的NSExceptionDomains列表中。

You can disable the security check entirely by turning on NSAllowsArbitraryLoads . 您可以通过打开NSAllowsArbitraryLoads完全禁用安全检查。 However, while useful for development purposes, it is always better to make your development environment work more like your release or production, thereby minimizing bugs and regressions. 但是,尽管对开发有用,但最好使开发环境更像发行版或生产版那样工作,从而最大程度地减少错误和回归。 (There is a bonafide reason to use NSAllowsArbitraryLoads in production, but if you are just talking to one backend, then you do not need it.) (在生产NSAllowsArbitraryLoads中使用NSAllowsArbitraryLoads是有原因的,但是如果您只是在与一个后端通信,则不需要它。)

You can learn about the settings for ATS here (go to ATS Configuration Basics). 您可以在此处了解有关ATS的设置(请参阅ATS配置基础)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM