dixkin
dixkin

Reputation: 821

iOS and XCode: What is the best way to store connection settings (i.e. URLs) for an iOS app?

We started with #defines pointing our app to dev, qa and live API servers. Well, that's rather lazy, not to mention painful to manage.

I'm thinking of storing default URL/connection settings in a .plist and creating dev|stage|live build configurations that will use the corresponding .plist for connections.

Is there a cleaner way to do this? How secure is a .plist?

Upvotes: 2

Views: 1251

Answers (2)

Karim
Karim

Reputation: 736

Here's how I handle it.

I have a single file where i store information like this. Lets call this file Configuration{h,m}

Within my .h (header) file, i declare the constants like this:

#pragma mark - API
extern NSString * const kAPIHost;
extern NSString * const kAPIVersion;

Within my .m (implementation) file, I finish it off with this

#pragma mark - API

#if TARGET_IPHONE_SIMULATOR
NSString * const kAPIHost = @"http://localhost/";
#else
NSString * const kAPIHost = @"http://liveserver.com";
#endif

NSString * const kAPIVersion = @"v2/";

What happens here is, it checks to see if i'm running on the sim, if i am, then it uses localhost, if it's running on device, it uses the live server.

Finally, I import Configuration.h in my precompiled header (PCH) file.

#import Configuration.h

Now, this file is available throughout your application without needing to import it in every single file. I use it like this.

NSURL * url = [NSURL URLWithString:[kAPIHost stringByAppendingString:[kAPIVersion stringByAppendingString:@"apiEndPoint/"]]];

The comment that troop231 posted sounds very interesting though. Maybe something I can implement at some time in the future. Always looking for more secure ways to do things :)

Upvotes: 1

Kamil Tustanowski
Kamil Tustanowski

Reputation: 201

You could store this in keychain - it's secure - but I don't know why You would want to do that.

You could use extern constants instead of defines: extern NSString *const in a class.

You can use preprocessor directives to help You manage the addresses: http://www.techotopia.com/index.php/Using_Objective-C_Preprocessor_Directives

You could create a singleton class that would for example read these urls from .plist at start and then hand it over to any class that is interested.

About security:

  • using singleton with .plist and shipping app would not reveal Your test / dev addresses.
  • using #define, externs and preprocessor directives i.e. #ifdef DEBUG would also not reveal Your dev / test addresses in production code.

Upvotes: 1

Related Questions