xcode 在iOS中在哪里设置环境变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11584089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:57:33  来源:igfitidea点击:

Where to set environmental variables in iOS?

iosxcodevariables

提问by jini

In my development I wish to hit a development URL when im testing but when the app is run on my iPhone, I want it to run on production URL. How do I get about setting this in Xcode?

在我的开发中,我希望在测试时访问开发 URL,但是当应用程序在我的 iPhone 上运行时,我希望它在生产 URL 上运行。我如何在 Xcode 中设置它?

Almost seems like I need to be able to set environmental variables (which in this case are different URLs).

几乎似乎我需要能够设置环境变量(在这种情况下是不同的 URL)。

Where do I set these up? Please hand hold me on the answer as I am fairly new

我在哪里设置这些?请握住我的答案,因为我是新手

Thanks

谢谢

回答by Erik

There are lots of ways to do this. My preferred method is to create a file called Constants.h that looks like this:

有很多方法可以做到这一点。我的首选方法是创建一个名为 Constants.h 的文件,如下所示:

//
//  Constants.h
//

#ifndef Constants_h
#define Constants_h

#pragma mark - Instances
#ifdef DEVELOPMENT
#define ROOT_URL @"http://127.0.0.1:8000"
#endif

#ifdef STAGING
#define ROOT_URL @"http://staging.example.com"
#endif

#ifdef PRODUCTION
#define ROOT_URL @"http://production.example.com"
#endif

Then, I just include it in my *-Prefix.pch file like this:

然后,我只是将它包含在我的 *-Prefix.pch 文件中,如下所示:

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "Constants.h"
#endif

In your build settings, set DEVELOPMENT, or STAGING, or PRODUCTION = 1.

在您的构建设置中,设置 DEVELOPMENT、STAGING 或 PRODUCTION = 1。

Now you can access things like ROOT_URL anywhere in your project.

现在,您可以在项目的任何位置访问 ROOT_URL 之类的内容。

Good luck!

祝你好运!

回答by bshirley

I prefer the #definemethod as well. For those less familiar, those are pre-processor directives and are set even before the compiler gets hold of the code.

我也更喜欢这种#define方法。对于那些不太熟悉的人,这些是预处理器指令,甚至在编译器获取代码之前就已设置。

Environment variablesare set at runtime of the program, and can be manipulated in the scheme editorof Xcode 4.x+. You could set up different schemes to hit different testing environments.

Environment variables在程序运行时设置,可以在scheme editorXcode 4.x+ 中操作。您可以设置不同的方案来满足不同的测试环境。

To access an environment variable at runtime from within your code, use the following:

要在运行时从代码中访问环境变量,请使用以下命令:

NSString *url = @"http://standardurl.com";
NSDictionary *env = [[NSProcessInfo processInfo] environment];
NSString *overrideURL = [env valueForKey:@"url"];
if (overrideURL != nil)
  url = overrideURL;

回答by nuxibyte

You can also set the different URLs in your plist file by adding new String rows if you don't want to use a constants file.

如果您不想使用常量文件,还可以通过添加新的字符串行在 plist 文件中设置不同的 URL。

(Not much to be gained here though I admit but depends on where you prefer to set up your urls)

(虽然我承认,但在这里获得的收益并不多,但这取决于您更喜欢在哪里设置网址)

In your myApp-plist file add new rows like this:

在您的 myApp-plist 文件中添加新行,如下所示:

"DevelopmentEndpoint" "String" "http://development.endpoint.com"

“DevelopmentEndpoint” “字符串” “http://development.endpoint.com”

"ProductionEndpoint" "String" "http://production.endpoint.com"

"ProductionEndpoint" "String" "http://production.endpoint.com"

In your code:

在您的代码中:

// Get endpoint from main bundle
#ifdef DEVELOPMENT
    NSString *endpoint = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"DevelopmentEndpoint"];
#else
    NSString *endpoint = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"ProductionEndpoint"];
#endif

In your build settings add DEVELOPMENT under the "Preprocessor Macros" section for Debug or Release etc.

在您的构建设置中,在调试或发布等的“预处理器宏”部分下添加开发。