× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Maintaining server URLs in application(iOS or android)

Sanman Borate
Dec 08, 2016
<h2 style="font-style:italic">Often as mobile application developer I need to be dependant on web server.Another thing is that there are lots of different urls for this webservices with hosts being different for different environments like development envt.,production envt..</h2> <h2 style="font-style:italic">I have seen a lot of people changing every url when they change envt alot similar to below part</h2> <h2 style="font-style:italic;"><strong>Note</strong> : I am just showing iOS code but I think android developers can get what I am trying to do or say :P</h2> <h2 style="font-style:italic;"><strong>Development envt.</strong></h2> <ol> <li> <h2 style="font-style:italic"><span style="color:#800000">#define LOGIN_URL @</span><span style="color:#0000CD">"https://myDevelopmentHost/rest/LoginAuthentication"</span></h2> </li> <li> <h2 style="font-style:italic"><span style="color:#800000">#define SOME_OTHER_SERVICE @</span><span style="color:#0000CD">"https://myDevelopmentHost/rest/SomeOtherServiceAfterLogin"​</span></h2> </li> </ol> <h2 style="font-style:italic"><span style="color:rgb(95, 99, 102)">​When shifting to <strong>production</strong>,change every development url like :</span></h2> <ol> <li> <h2 style="font-style:italic"><span style="color:#800000">#define LOGIN_URL @</span><span style="color:#0000CD">"https://myProductionHost/rest/LoginAuthentication"</span></h2> </li> <li> <h2 style="font-style:italic"><span style="color:#800000">#define SOME_OTHER_SERVICE @</span><span style="color:#0000CD">"https://myProductionHost/rest/SomeOtherServiceAfterLogin"​</span></h2> </li> </ol> <h2 style="font-style:italic">This is <strong>fine</strong> in case of <strong>2-5</strong> urls,but what if you have <strong>20-25 urls???</strong> Changing every URL isn't fun job and takes time</h2> <h2 style="font-style:italic"><strong>Quick tip :</strong> What I suggest is to do it as follows,</h2> <ol> <li> <h2 style="font-style:italic">Keep host different than remaining part like</h2> </li> </ol> <ul> <li> <h2 style="font-style:italic"><span style="color:#8B4513">#define HOST_URL</span> <span style="color:#800000">@</span><span style="color:#0000FF">"https://myDevelopmentHost/rest/"</span> and</h2> </li> <li> <h2 style="font-style:italic"><span style="color:#800000">#define LOGIN_URL @</span><span style="color:#0000FF">"LoginAuthentication"</span></h2> </li> <li> <h2 style="font-style:italic"><span style="color:#800000">#define SOME_OTHER_SERVICE @</span><span style="color:#0000CD">"SomeOtherServiceAfterLogin"</span><span style="color:rgb(95, 99, 102)">​</span></h2> </li> </ul> <h2 style="font-style:italic">   2. And <strong>when</strong> <strong>using</strong> <strong>them</strong> while calling a service <strong>append</strong> <strong>them</strong> <strong>like</strong>, <strong>HOST_URL+LOGIN_URL</strong></h2> <h2 style="font-style:italic"><span style="color:rgb(95, 99, 102)">Now lets say I need to change my app environment to production,what I'll do is simply change host url i.e</span></h2> <h2 style="font-style:italic"> </h2> <ul> <li> <h2 style="font-style:italic"><span style="color:rgb(95, 99, 102)"><strong>from</strong> </span><span style="color:#800000">#define HOST_URL @</span><span style="color:#0000CD">"https://myDevelopmentHost/rest/"</span> <strong>to</strong></h2> </li> <li> <h2 style="font-style:italic"><span style="color:#800000">#define HOST_URL @</span><span style="color:#0000CD">"https://myProductionHost/rest/"</span> and <strong>build</strong></h2> </li> </ul> <h2 style="font-style:italic">No need to touch any URLs,<strong>just</strong> <strong>switch the host</strong> <strong>from one mode to another</strong>.</h2> <h2 style="font-style:italic">Hope it helps you save time :)</h2> <h2 style="font-style:italic"> </h2>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus