iOS: Simple way to manage REST end points

后端 未结 4 1210
既然无缘
既然无缘 2021-01-24 07:28

Our REST based application can be used for testing on multiple internal environments each with a different REST end point. Is there a simple way to set up environment level co

4条回答
  •  误落风尘
    2021-01-24 08:09

    I found that creating different Scheme and Configuration for your project works best. My setup is as follow:

    I usually have 3 different scheme, MyApp-dev, MyApp-staging and MyApp.

    Each of the scheme i created User-Defined-Attribute to have different string appending to my Bundle Display Name. So it can concurrently appear on my iOS device as MyApp-d, MyApp-s and MyApp. Each also have its own Bundle ID Then I create custom flags for each of them.

    So in my Routes.swift files i have something like this at the top:

    #if PRODUCTION
        static let hostName = "http://production.com/api/v1/"
    #elseif STAGING
        static let hostName = "http://staging.com/api/v1/"
    #else
        static let hostName = "http://development.com/api/v1/"
    #endif
    

    There is quite a few ways in how to update different hostname. But ultimately creating different Scheme and Configuration is always the first step.

    Here is a few links that might help you get started:

    https://medium.com/@danielgalasko/change-your-api-endpoint-environment-using-xcode-configurations-in-swift-c1ad2722200e#.o6nhic3pf

    http://limlab.io/swift/2016/02/22/xcode-working-with-multiple-environments.html

提交回复
热议问题