Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

angular - Nativescript localhost http call fails on iOS

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([]);
            })
        );
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...