在iOS中设置环境变量的位置? [英] Where to set environmental variables in iOS?
问题描述
在我的开发中,我希望在进行测试时点击开发网址,但是当我的iPhone上运行该应用时,我希望它能够在生产网址上运行。我如何在Xcode中设置它?
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)。
Almost seems like I need to be able to set environmental variables (which in this case are different URLs).
我在哪里设置这些?请把握住我的答案,因为我相当新。
Where do I set these up? Please hand hold me on the answer as I am fairly new
谢谢
推荐答案
有很多方法可以做到这一点。我首选的方法是创建一个名为Constants.h的文件,如下所示:
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
//
#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
然后,我将它包含在我的* -Prefix.pch文件中,如下所示:
Then, I just include it in my *-Prefix.pch file like this:
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constants.h"
#endif
在构建设置中,设置开发,或STAGING,或PRODUCTION = 1.
In your build settings, set DEVELOPMENT, or STAGING, or PRODUCTION = 1.
现在,您可以在项目的任何位置访问ROOT_URL等内容。
Now you can access things like ROOT_URL anywhere in your project.
祝你好运!
这篇关于在iOS中设置环境变量的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!