1

I am using Nativescript sidekick cloud build on windows to test my app on iOS - I have also connected my iPhone.

According to this post I am sending my http requests to localhost but they keep failing with this error.

Http failure response for http://localhost:52553/api/rewards/all: 0 Unknown Error
Error: Could not connect to the server.

Here is my implementation of rewards service

import { Injectable } from "@angular/core";
import { HttpClient } from "@angular/common/http";

import { Observable, of } from 'rxjs';
import { SimpleReward } from "../models/simple-reward";
import { take, catchError } from "rxjs/operators";

@Injectable()
export class RewardsService {

    private baseUrl = "http://localhost:52553/api/rewards";

    constructor(private http: HttpClient) { }

    getAllRewards(): Observable<SimpleReward[]> {
        const url = `${this.baseUrl}/all`;
        return this.http.get<SimpleReward[]>(url).pipe(
            catchError((e) => {
                console.error(e);
                return of([]);
            })
        );
    }
}

Please guide what I am doing wrong here?

shobhit vaish
  • 951
  • 8
  • 22

2 Answers2

9

Add localhost as an exception to your app/App_Resources/iOS/Info.plist file to allow insecure (non-HTTPS) communication with the http://localhost domain:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

This is preferable to allowing insecure communication with all domains.

EDIT: Also, remember that you must rebuild your app after editing that Info.plist file! Its changes cannot simply be live-synced or hot module reloaded like JS.

Jamie Birch
  • 5,839
  • 1
  • 46
  • 60
  • Thanks for the response. I added it but got the same error. – shobhit vaish May 15 '19 at 12:02
  • 1
    @shobhitvaish You'll need to rebuild the app after altering the `Info.plist`; did you do that? Can do a clean build to avoid doubt. – Jamie Birch May 15 '19 at 13:14
  • I tried clean build but no luck. I am sure my localhost is working – shobhit vaish May 16 '19 at 06:06
  • 1
    Wait a minute. You're running `localhost` on your desktop, aren't you? Try running the app on your simulator instead of a physical device: `tns run ios --emulator`. Your iPhone won't have access to your desktop web server's `localhost`. If you want to connect to that from your physical iPhone, you'll need to turn on web sharing on your desktop (maybe also disable a firewall port) and connect to a specific IP from your iPhone rather than `localhost` and provide the corresponding `NSExceptionDomains` entry for it. – Jamie Birch May 16 '19 at 11:57
  • I see. I am on Windows so I can't run on emulator. Let me try the second approach. – shobhit vaish May 16 '19 at 12:35
  • 1
    So I finally got it working. Thanks for the guidance! – shobhit vaish May 17 '19 at 06:43
1

You can disable the security or add localhost as exception in your info.plist.

<key>NSAppTransportSecurity</key>  
 <dict>  
      <key>NSAllowsArbitraryLoads</key><true/>  
 </dict>
Narendra
  • 4,514
  • 2
  • 21
  • 38