在 Xcode 中为不同环境组织 API 基本 URL 的最佳方法 [英] Best way to organize API base URL for different environment in Xcode

查看:24
本文介绍了在 Xcode 中为不同环境组织 API 基本 URL 的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的 API 有 3 个环境Dev、Stage 和 Prod.我的 Xcode 中有 3 个针对 3 个环境设置的目标和方案,它们具有不同的构建、代码签名、配置文件和 plist.

We have 3 environments Dev, Stage and Prod for our API. I have 3 targets and schemes for 3 environments setup in my Xcode which have different builds, code signing, profiles and plist.

如何对 API 基本 URL 执行相同的操作?现在我有 1 个前缀文件.因为它是 1 个项目,所以所有 3 个目标都使用相同的文件,每次我为不同的环境构建时,我都必须更改注释.

How do I do the same for API base URL? right now I have all in 1 prefix file. Because it is 1 project so all 3 targets are using the same file and I have to change the comment out every time I build for different environment.

//DEV
#define SERVER_URL @"http://api.dev.com/api/"
//STAGE
#define SERVER_URL @"http://api.stage.com/api/"
//PROD
#define SERVER_URL @"http://api.prd.com/api/"

在 Xcode 中为不同目标环境组织 API 基本 URL 的最佳方法是什么?

What is the best way to organize API base URL for different target environment in Xcode?

谢谢,

推荐答案

我四处寻找,并为我找到了一个简单而有效的解决方案.

I played around and found a simple yet effective solution for me.

我将 API 基本 URL 作为一个值放在每个目标的 plist 中,键为 @"ServerURL".然后在与 API 接口的单例类上,我把它放在 init

I put the API base URL as a value in the plist of each target with the key @"ServerURL". Then on the singleton class interfacing with the API, I put this in the init

NSDictionary* infoDictionary = [[NSBundle mainBundle] infoDictionary];
serverURL = [infoDictionary objectForKey:@"ServerURL"];

根据我正在构建的目标,我有正确的 serverURL.

There I have the correct serverURL based on which target I'm building.

这篇关于在 Xcode 中为不同环境组织 API 基本 URL 的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆