如何允许应用在 ios 上访问互联网?

How to allow an app to access internet on ios?

我的 Nativescript 应用程序在 Android 上运行良好,但在 ios 上无法连接到互联网。安装或模拟。

这是我的商店(我使用互联网的地方):

import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios/dist/axios';

const api = axios.create({
  baseURL: 'http://asdfasdf.compute.amazonaws.com',
  timeout: 2000,
});

Vue.use(Vuex);
export default new Vuex.Store({
  strict: process.env.NODE_ENV !== 'production',
  state: {
    name: null,
    err: null,
  },
  mutations: {
    initialize(state, name) {
      state.name = name;
    },
    err(state, txt) {
      state.err = txt;
    },
  },
  actions: {
    init({commit}) {
      api
        .get('/name/')
        .then(response => {
          commit('initialize', response.data);
        })
        .catch(function(error) {
          commit('err', error);
        });
    },
  },
});

我在 android 安装中很好地加载了名称, 但是在 ios 模拟器和 iPhone 安装上我得到:

Error: Request failed with status code null

有什么办法可以解决这个问题吗?我在任何地方都没有收到任何错误或异常,如果我断开 phone 与互联网的连接,我也会收到完全相同的错误。我没有使用任何代理。

一个可能的问题是 ATS 你可以阅读 here

您需要做的是将以下代码段添加到 app/App_Resources/iOS/Info.plist。 里面有一个<dict>标签,里面有很多内容。您需要做的就是将它放入 <dict>

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSAllowsArbitraryLoads</key>
  <true/>
</dict>

所以你会得到类似的东西:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
    # snippet goes here
    .
    .
    .
    </dict>
</plist>

如果您的服务器使用的是自签名证书,您也需要这样做。

编辑允许特定域而不是所有域的 Info.plist 文件后问题消失了。我听说如果您尝试在应用商店上传您的应用程序,它会在 iOS 10 + 如果您允许所有 http 域。

这是我的应用程序传输安全

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
            <dict>
                <key>specificDomain.com</key>
                <dict>
                    <key>NSIncludesSubdomains</key>
                    <true/>
                    <key>NSExceptionAllowsInsecureHTTPLoads</key>
                    <true/>
                    <key>NSExceptionRequiresForwardSecrecy</key>
                    <false/>
                </dict>
            </dict>
        <key>NSAllowsLocalNetworking</key>
        <true/>
    </dict>