Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. For some reason I cannot get my app to make network requests to my backend api on AWS. My code works as expected in the simulator when I hit the backend api on my local dev environment with localhost:3000/auth/login/. Below are the results of my efforts.

HTTP Class Post Method:

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. 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.

enter image description here

Running the same exact call from Postman works as expected and can be seen below:

enter image description here

Any assistance would be greatly appreciated. Thanks you in advance.

like image 812
Aaron Avatar asked May 22 '26 17:05

Aaron


1 Answers

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.

You can disable the security check entirely by turning on 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.)

You can learn about the settings for ATS here (go to ATS Configuration Basics).

like image 63
greymouser Avatar answered May 24 '26 07:05

greymouser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!