Is it possible to access a locally running api from the iOS Simulator all from Windows?

james1301james1301 Member ✭✭
edited August 2018 in Xamarin.Forms

I have an API server project running on my local windows machine (at the moment just running on a localhost address, can be changed).
I am then trying to test my Xamarin Forms app which tries to connect to this API on the iOS simulator.

How is it best to do this?

From googling I have found that if I was doing this all from the Mac it might just work? But how do I get this to work from Windows? If I run the UWP app, it all works as expected, because it is running all on my machine.

At present I am having to publish my API and access it that way, but that isn't the ideal way, just seems unnecessary when I'm trying to test things quickly with debugging.

Answers

  • ManojkumarMaliManojkumarMali USMember ✭✭✭

    I think, If your windows machine and mac machine are on same network then you might test local API calls from app.
    Don't know exactly but you can try. :)

  • james1301james1301 Member ✭✭

    I have been trying, hence my question, I don't know how to get it to work.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    @james1301

    It would definitely help if your Windows PC is on the same subnet as your Mac. Then you would need to specify the IP address of the workstation that is running your API service instead of using localhost url (which just means the service is running on the same machine that the code is running on, which in the case of an iOS simulator "localhost" is the iOS Simulator, and I assume your service is not running on the iOS simulator :-) So you need to replace "localhost" in your code with the IP address of your Windpws PC).

    Also you will need to open firewall rules on your Windows PC to allow remote access to the API service. See our guide on using WCF service with Xamarin, especially the section on configuring remote access which shows how to allow remote requests:
    https://docs.microsoft.com/en-us/xamarin/cross-platform/data-cloud/web-services/walkthrough-working-with-wcf#configuring-remote-access-to-iis-express

  • Ahsan_SiddiqueAhsan_Siddique PKMember ✭✭✭✭

    @james1301 said:
    I have an API server project running on my local windows machine (at the moment just running on a localhost address, can be changed).
    I am then trying to test my Xamarin Forms app which tries to connect to this API on the iOS simulator.

    How is it best to do this?

    From googling I have found that if I was doing this all from the Mac it might just work? But how do I get this to work from Windows? If I run the UWP app, it all works as expected, because it is running all on my machine.

    At present I am having to publish my API and access it that way, but that isn't the ideal way, just seems unnecessary when I'm trying to test things quickly with debugging.

    Using the application from your own machine with localhost

    When you want to test the application using an emulator with the real API using localhost, you'll need to follow a couple of steps to get things running. By default, localhost on your machine won't be accessible from the emulator (Android or iOS). Please note that several solutions exist to work around this problem, the below solution is describing the way I work.

    1. Download SharpProxy from https://github.com/jocull/SharpProxy
    2. Open the Xamarin solution. In there, right click on the API and select Debug -> Start new instance. The API will launch. Take note of the port number it's running on.
    3. Run the SharpProxy project and enter the port number on which your ASP.NET Core API is running in the Internal port field.
    4. Take the IP address (also shown in SharpProxy) + the external port number and now go to the Xamarin project. Search for the ApiConstants class and in there, you'll find the IP I've used (192.168.1.35:5000). Replace that with the IP + port number for your machine (shown in SharpProxy).
    5. With the API and SharpProxy now still running, you can now launch your Xamarin.Android or Xamarin.iOS project. From Visual Studio, again rightclick and select Debug --> Start new instance.

    Your Xamarin application should now be able to access the API.

  • james1301james1301 Member ✭✭

    @Ahsan_Siddique said:

    @james1301 said:
    I have an API server project running on my local windows machine (at the moment just running on a localhost address, can be changed).
    I am then trying to test my Xamarin Forms app which tries to connect to this API on the iOS simulator.

    How is it best to do this?

    From googling I have found that if I was doing this all from the Mac it might just work? But how do I get this to work from Windows? If I run the UWP app, it all works as expected, because it is running all on my machine.

    At present I am having to publish my API and access it that way, but that isn't the ideal way, just seems unnecessary when I'm trying to test things quickly with debugging.

    Using the application from your own machine with localhost

    When you want to test the application using an emulator with the real API using localhost, you'll need to follow a couple of steps to get things running. By default, localhost on your machine won't be accessible from the emulator (Android or iOS). Please note that several solutions exist to work around this problem, the below solution is describing the way I work.

    1. Download SharpProxy from https://github.com/jocull/SharpProxy
    2. Open the Xamarin solution. In there, right click on the API and select Debug -> Start new instance. The API will launch. Take note of the port number it's running on.
    3. Run the SharpProxy project and enter the port number on which your ASP.NET Core API is running in the Internal port field.
    4. Take the IP address (also shown in SharpProxy) + the external port number and now go to the Xamarin project. Search for the ApiConstants class and in there, you'll find the IP I've used (192.168.1.35:5000). Replace that with the IP + port number for your machine (shown in SharpProxy).
    5. With the API and SharpProxy now still running, you can now launch your Xamarin.Android or Xamarin.iOS project. From Visual Studio, again rightclick and select Debug --> Start new instance.

    Your Xamarin application should now be able to access the API.

    Any chance this could work with https?

Sign In or Register to comment.