iOS 上的 Nativescript localhost http 调用失败

sho*_*ish 1 nativescript angular

我在 Windows 上使用 Nativescript sidekick cloud build 在 iOS 上测试我的应用程序 - 我还连接了我的 iPhone。

根据这篇文章,我正在向 localhost 发送我的 http 请求,但他们一直因这个错误而失败。

http://localhost:52553/api/rewards/all 的Http 失败响应:0 未知错误
错误:无法连接到服务器。

这是我对奖励服务的实现

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([]);
            })
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

请指导我在这里做错了什么?

Jam*_*rch 7

localhost作为例外添加到您的app/App_Resources/iOS/Info.plist文件以允许与http://localhost域的不安全(非 HTTPS)通信:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>localhost</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>
Run Code Online (Sandbox Code Playgroud)

这比允许与所有域的不安全通信更可取。

编辑:另外,请记住,您必须在编辑该Info.plist文件后重建您的应用程序!它的更改不能像 JS 那样简单地实时同步或热模块重新加载。