Search code examples
iosvue.jsaxiosnativescriptnativescript-vue

How to allow an app to access internet on ios?


My Nativescript application works perfectly fine on Android but it doesn't connect to internet on ios. Either installed or simulated.

This is my store(where I use the internet):

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);
        });
    },
  },
});

I get the name loaded just fine on my android installation, But on ios emulator and iPhone installation I get:

Error: Request failed with status code null

Any thoughts how I can fixed this? I don't get any errors or exceptions anywhere, also I get the exact same error if I disconnect my phone from the internet. I'm not using any proxy.


Solution

  • One possible problem is ATS you can read here

    What you need to do is adding the following snippet into app/App_Resources/iOS/Info.plist. There is a <dict> tag in there with a lot of content in it. All you need to do is putting this inside that <dict>

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

    So you will get something like:

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

    You also need to do this if your server is using a self-signed certificate.