开发者

Automatically use different constant for development

My iOS App uses a specific server for production: http://mydomain.com/serv开发者_运维技巧erapi. While developing I would like to change this to http://localhost/serverapi. What is an elegant way to tell XCode to use the local URL by default.

Obviously it's very important, that a production release contains the production URL (Archiving in XCode.

Another use case would be to change the icon or app identifier while in development, to be able to tell the app apart from the version which might already be installed on the device.


I use macros for this (as Pablo suggested), but if you have a separate Info.plist file for different build configurations (as I do), then you could have a custom entry in your Info.plist and access it like so:

 [[[NSBundle mainBundle] infoDictionary] objectForKey:@"MyAppURL"]

However, macros are likely faster at runtime as they are compiled in. This is just another option.

Below is an image of the project setting.

Automatically use different constant for development


You can use C language MACROS.

Like this:

#ifdef DEBUG
#define URL @"http://localhost/serverapi"
#else
#define URL @"http://mydomain.com/serverapi"
#endif

Then, in your code, you just use the defined constant:

NSLog(@"Hello, my current url is %@", URL);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜