How do I configure full URLs in xcconfig files

2019-01-14 10:35发布

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.

6条回答
我只想做你的唯一
2楼-- · 2019-01-14 11:10

You can use backslashes to escape:

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

EDIT

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

查看更多
Evening l夕情丶
3楼-- · 2019-01-14 11:11

Just declare

SIMPLE_SLASH=/

Then your URL becomes

http:$(SIMPLE_SLASH)/www.stackoverflow.com
查看更多
我命由我不由天
4楼-- · 2019-01-14 11:12

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

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}
查看更多
做个烂人
5楼-- · 2019-01-14 11:25

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".

查看更多
Summer. ? 凉城
6楼-- · 2019-01-14 11:27
SLASH=/

API_URL=http:$(SLASH)/endpoint.com
查看更多
我想做一个坏孩纸
7楼-- · 2019-01-14 11:30

Here's a simple workaround:

WEBSITE_URL = https:/$()/www.example.com
查看更多
登录 后发表回答