views:

111

answers:

4

Hello all ,

I am developing an iPhone application in which I am fetching data from a web service currently located on a local server in LAN.

Now I am fetching my data located on a local server with url something like this :

http:\192.168.0.150\service\service.asmx\dataFetchingMethod

But In future if my url changes i.e server something like this :

http:\www.anotherDomain.com\service\service.asmx\dataFetchingMethod

and

http:\www.anotherURL.com\service\service.asmx\dataFetchingMethod

then how I can change It dynamically .

Currently I have hard coded the url in the application ...

Please specify if you have any tutorial or example ..

+1  A: 

You should write a class that reads properties from a file (such as a plist).

That way whenever the app runs, it'll read its configuration from the plist, and make calls to the service URL specified. This can be changed without recompiling the app.

Jasarien
+1  A: 

You're going to need to connect to some kind of resource that either never changes or changes very infrequently. I see a couple of different approaches.


Proxy Approach

You could set up a master server (http://masterurl.com/) that proxies your request to another server. The idea here is that the master server never changes, but the machine that it proxies to could change at any time. For example:

Assuming your iPhone app connects to http://masterurl.com/:

  1. All requests to http://masterurl.com/ are proxied to http://someotherurl.com/.

  2. At some point in the future, you need to replace http://someotherurl.com/ with http://snazzynewurl.com/.

  3. Now you can just change http://masterurl.com/ to proxy to http://snazzynewurl.com/.

To your application's user nothing has changed and you don't have to update your app and re-submit it for review.


Location File Approach

You could get the location from a file, like a txt or xml file.

Assuming your file lives at http://someurl.com/location.txt and the contents are simple:

inside location.txt:

http://someotherbaseurl.com/

Your app would then read in this text file and use http://someotherbaseurl.com/ as a base url for the rest of your app. Now if you need to make a change to the URL, you can just update the text file.

If you're taking the location file approach, I'd recommend hosting the file using Amazon S3 or Rackspace Cloud Files so that the text file is available over a URL on their content delivery network.

I'm sure there are other ways to tackle this. Either way you'll need a master URL that changes never or seldom. Otherwise you'll run into a situation where you'll have to re-compile and re-submit your app.

richleland
I think the second one approach is for me
hib
+2  A: 

Application Preferences are made for this:

- (BOOL) getDataFromWebserver
{

    NSString *url = [[NSUserDefaults standardUserDefaults] stringForKey:@"dataUrl"];

    if(url == nil)
    {
     [self setDefaultPrefs];
     url = [[NSUserDefaults standardUserDefaults] stringForKey:@"dataUrl"];
    }
    [self fetchData:url];//to implement
}

- (void)setDefaultPrefs
{
     NSDictionary *appPrefs = [NSDictionary dictionaryWithObjectsAndKeys:
              @"hardCodedUrl", @"dataUrl", nil];

 [[NSUserDefaults standardUserDefaults] registerDefaults:appPrefs];
 [[NSUserDefaults standardUserDefaults] synchronize];  
}

Read more

Kai
Thanks . I think you have got the point .
hib
+1  A: 

Isn't this exactly what DNS is for? You specify a single domain name, that re-directs where you like... it's not much help in debugging before you have the domain set up, but get something set up now and re-direct where you like.

Kendall Helmstetter Gelner