Webserver on the go: Using web services on localhost for your iOS app tests

Recently I commute a lot, and I also work on an iOS project which needs to download data from a web server. When I had ran out of my mobile data package I realised that I need cheaper solution in order to test the data connection and the data download in my iOS App. If you are in the lucky situation when you can have access to the website code and database, which will serve your iOS application data needs, in most of the cases you can easily install it on your Mac. If getting the source code and at least the test database is not an option for any reason, then other solution is to mock the required functionality. For example, if the output of the backend web service is JSON file, you can easily create one or more static files, at least for unit testing. Having a mocked RESTFul API can be achieved in the same way, which is enough to test your code functionality. If you need some mocking for RESTFul, I would suggest to take a look here: http://appcarvers.com/blogs/entry/writing-mock-api-in-php, or use http://wiremock.org.

Back to my basic problem, after a quick search on the internet and I found that localhost will be always resolved as your Mac address in the iOS simulator, plus your simulator’s OS always uses the same /etc/hosts file as your guest machine operating system. The only thing you need to do in order to have a web server on your Mac is either switch on the built-in apache server in your OS X and get MySQL package (you can find a very well detailed setup process here: http://jason.pureconcepts.net/2015/10/install-apache-php-mysql-mac-os-x-el-capitan/, or download MAMP from http://mamp.info to fire up a full tailored Apache-MySQL-PHP stack.

If you have access to the website source and the database dump, download them and set them up on your Mac, to run from (most probably) on the localhost, and you are done. As I mentioned before, it will even work with the old style /etc/hosts file hack as well, which means that you can use different hostnames as well (for example: petermolnar.local) and with the capabilities of VirtualHosts in Apache you can also configure your web server to use those “fake hosts”. Please note, that the free version of MAMP is not easily support VirtualHosts out of the box.

I think if you have done some web development on your Mac before it won’t be new for you, but the way how the simulator and the guest system sharing the network subsystem was a bit new for me, and also very useful in my current iOS development project.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.