Search code examples
iphonedynamic-data

Change server without recompiling iPhone application, how?


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.


Solution

  • 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