How do I configure full URLs in xcconfig files

2019-01-14 10:33发布

问题:

I have an xcconfig file which contains a configuration for which server my app should hit. In debug mode, this will be a different server than for release builds.

The problem I have is that a URL of the form http://www.stackoverflow.com is treated as a comment after the double slash. So the string I get in code is 'http:'

I have read that I can put a -traditional build flag on Info.plist, I was wondering if someone else has had a similar issue and has solved it?

Thanks.

回答1:

Here's a simple workaround:

WEBSITE_URL = https:/$()/www.example.com


回答2:

I also could not figure out how to use a double slash in a xcconfig file. But I found a workaround in

  • Re: Double forward slashes in .xcconfig-defined build settings

from the Xcode-users mailing list: In the xcconfigfile, save the URL without the http scheme:

MYURL = stackoverflow.com

In the Info.plist, set the property value to

http://${MYURL}


回答3:

Just declare

SIMPLE_SLASH=/

Then your URL becomes

http:$(SIMPLE_SLASH)/www.stackoverflow.com


回答4:

SLASH=/

API_URL=http:$(SLASH)/endpoint.com


回答5:

You shouldn't use a xcconfig file for this setting.

A xcconfig file is not a "normal" header or module file which is the input of the preprocessor and eventually the input for the compiler. It's nowhere specified how the xcconfig file parser treats character encoding, whether it recognizes escape sequences, whether it expands macros, and how character literals are defined and much more.

It's far better in this case, to have a "config.h" header file and use a conditional based on a preprocessor definition:

#if defined (DEBUG)
    NSURL* url = ...
#else
    NSURL* url = ...
#endif

Here, DEBUG is defined for Debug configuration by default. You may #define any other definition in the build settings under "Preprocessor Macros".



回答6:

You can use backslashes to escape:

URL = "http:\/\/mydomain.com"

EDIT

Don't forget to Clean your project before re-build.