3

I've got the following constant in the code:

static NSString* const MyUrl = @"www.myurl.com";

Is it possible at all to create a user-defined setting and assign a value which can replace the value of MyUrl const at run time or during archive?

My situation is the following: I have a project with various targets. Each target points to a different URL in the code. It would be great if I could manage the URL through a user-defined setting rather than having to change the code every time I change the target.

Shripada
  • 6,296
  • 1
  • 30
  • 30
user1309226
  • 739
  • 1
  • 10
  • 31

4 Answers4

1

Consider using info.plist for storing such values.

Arik Segal
  • 2,963
  • 2
  • 17
  • 29
0

No you could not change the value of const variable.

You can change the value of URL string by simply NSString *myURL = @"www.url.com";

and use the global variable in AppDelegate and use that from any where in project.

Jatin Chauhan
  • 1,107
  • 1
  • 8
  • 21
0

You can use pre processor macros for this purpose. Goto to Xcode>Project>Target>Build Settings>Preprocessor Macros. Then for every build configuration (Debug, release, etc) add a target specific macro. In your source code, you can now identify your target, by just referring to the macro using #ifdef.

Example:

#ifdef TARGET1
static NSString* const MyUrl = @"www.myurl.com";
#endif

#ifdef TARGET2
static NSString* const MyUrl = @"www.myur2.com";
#endif

The following image depicts the declaration of TARGET macro in build settings-

enter image description here

Shripada
  • 6,296
  • 1
  • 30
  • 30
-4

I think you need to use a database or any other source out of your app. And fetch the URL string whenever it changes and you can use your NSString variable inside the code. You can use parse for your need (However, Parse will stop their service next year but there are lots of free online DBs you can use). With this method you don't need to change your code to update URL strings. I hope I could get what you need correctly.

icould
  • 315
  • 3
  • 9
  • Strange to have "-" for my answer. When I checked the other answers and the question I need to ask "how will you change the URL string after publishing the app without releasing a new binary?". Either question is not right or I am missing something here. Question seems weird now if I check the answers. "User-defined" phrase is something different than I know... – icould Mar 23 '16 at 13:35
  • 1. This is about swapping targets and is before releasing a new binary. 2. User does not mean an "end user" or "application user" and in this context it means a build target since the target is the one who uses the settings. – Matic Oblak Mar 24 '16 at 15:20