当前位置:主页 > 资料 >

Use multiple target / scheme for different server environmen
栏目分类:资料   发布日期:2018-08-03   浏览次数:

导读:本文为去找网小编(www.7zhao.net)为您推荐的Use multiple target / scheme for different server environment URL,希望对您有所帮助,谢谢! Do you comment and uncomment different URLs to switch environment like this? stru

本文为去找网小编(www.7zhao.net)为您推荐的Use multiple target / scheme for different server environment URL,希望对您有所帮助,谢谢!

欢迎访问www.7zhao.net



Do you comment and uncomment different URLs to switch environment like this?

去找(www.7zhao.net欢迎您

struct Endpoints {
    // development server URL
    static let baseURL = "https://fluffa.herokuapp.com/"
    
    // production server URL
    // static let baseURL = "https://randomfox.ca/"

    static func randomFox() -> URL {
        return URL(string: Endpoints.baseURL + "floof")!
    }
} 
copyright www.7zhao.net

While this approach is straightforward, it is very prone to human mistake, what if you forgot to switch back to production server when archiving? Ever shipped an app which connects to development server to the App Store? (I have, and I am definitely not proud about it)

欢迎访问www.7zhao.net

In this post we will use target / scheme in Xcode for separating different server environment. The technique shown in this post is only applicable for Swift and Xcode 8+.

内容来自www.7zhao.net

This post assume that you already knew what isURLSession. 欢迎访问www.7zhao.net

Table of contents :

copyright www.7zhao.net

  1. Duplicate current target in project
  2. Add custom flag to the development target app
  3. Using flag and preprocessor to switch server URL
  4. Use different app icon for different target

Download the sample project to follow along the post

Get sample Xcode project which uses two scheme / target, one for development and another for production server

Duplicate current target in project

First, we will go to the Project Setting, select your current App Target, right-click and select " Duplicate ".

去找(www.7zhao.net欢迎您

copyright www.7zhao.net

After clicking Duplicate , you will see another target has been created, with a "copy" at the end of its name. We can use this newly created target for development server environment. Let's double click it to rename the target name to "(AppName) Development".

copyright www.7zhao.net

copyright www.7zhao.net

We will also change the Display name so the app will have a different name on the iPhone then we can differentiate the development and production app.

www.7zhao.net

We will also change the bundle identifier to another name. This is because no two apps can share the same bundle identifier in the same device. If two apps share the same bundle identifier, the iOS device / simulator will remove the first app and replace it with the second app if you install the second app in the same device. 去找(www.7zhao.net欢迎您

When we duplicate a target, Xcode will create a new scheme for us as well, the newly created scheme will use the new target we created by default. 去找(www.7zhao.net欢迎您

We can change the scheme like this:

内容来自www.7zhao.net

You can change the scheme and build the app. After building both scheme, you will see two apps appear in the simulator. (eg: MultiScheme and MultiScheme Development )

www.7zhao.net

www.7zhao.net

Next, we will proceed to rename the scheme so that it will share the same name as target. Similar as previous step, click the scheme beside build button and select "Manage Scheme".

内容来自www.7zhao.net

本文来自去找www.7zhao.net

Then click on the "(appName) copy" and press Enter to rename it. We will rename it to "(appName development) to match the Target name. copyright www.7zhao.net

本文来自去找www.7zhao.net

Add custom flag to the development target app

Go to project settings, select the development target, and follow the instruction below to add a custom flag to the development target: 内容来自www.7zhao.net

Type in "DEVELOPMENT" for the flag and press Enter. Now you will have a "DEVELOPMENT" flag when you build and run the development target. Do the same for the "Release" row, so both "Debug" and "Release" row will have "DEVELOPMENT" flag. www.7zhao.net

欢迎访问www.7zhao.net

When you start a new iOS project, Xcode will create two build configuration, Debug and Release , Xcode will also add a "DEBUG" flag into the Debug build configuration by default. 内容来自www.7zhao.net

What are these build configuration used for?

copyright www.7zhao.net

In the "Manage Scheme" Window, double click into the scheme of your app, and you will see a list of actions on the left side (eg: Build, Run, Test etc). copyright www.7zhao.net

Select Run on the left side and you will see "Debug" is selected for the "Build Configuration" by default. 内容来自www.7zhao.net

This mean that if you build and run the app in simulator / your plugged iPhone, the app will use the "Debug" build configuration, and it will have the "DEBUG" flag. Remember we have also added the "DEVELOPMENT" flag previously in the Debug row for the development target app? If we build and run this app, the app will have both "DEBUG" and "DEVELOPMENT" flag. 本文来自去找www.7zhao.net

Now select Archive on the left side, and you will see "Release" is selected for the "Build Configuration". This mean that when we archive the app for App Store submission, the app will use the "Release" build configuration.

copyright www.7zhao.net

If we didn't put "DEVELOPMENT" flag into the Release row earlier, it won't appear in the app if we archive this app and submit to App Store / Testflight (which might make the app use production server instead of development).

去找(www.7zhao.net欢迎您

本文来自去找www.7zhao.net

Using flag and preprocessor to switch server URL

Now we gotten the flag ready, we can use it to switch between server URL when a different target is selected. 本文来自去找www.7zhao.net

In your networking/endpoint code, add the #IF DEVELOPMENT preprocessor check like this :

copyright www.7zhao.net

struct Endpoints {
    #if DEVELOPMENT
    static let baseURL = "https://fluffa.herokuapp.com/"
    #else
    static let baseURL = "https://randomfox.ca/"
    #endif
	
    static func randomFox() -> URL {
        return URL(string: Endpoints.baseURL + "floof")!
    }
} 本文来自去找www.7zhao.net 

Code with # (youngster calls it hashtag?) in front are known as preprocessor. What preprocessor does is that it will process the source code before it is compiled into an app. The #if , #else and #endif preprocessor will check if a flag is defined for the app or not during preprocessing stage and modify the source code based on the condition. The flow looks like this:

欢迎访问www.7zhao.net

欢迎访问www.7zhao.net

Notice that during preprocessing stage, the preprocessor will check the flag to use different line of baseURL. So now you have it! You can change the target to development during development to use the dev server, and archive the production target for App store submission. www.7zhao.net

Use different app icon for different target

Now we have two app target for development and production, but they share the same icon which can be confusing sometimes. Let's use another app icon for the development target.

去找(www.7zhao.net欢迎您

Go to Assets.xcassets , click the "+" below and select "App Icons & Launch Images" > "New iOS App Icon". Put in your icon, then rename it.

内容来自www.7zhao.net

Then go to project settings, select the development target app, go to "Build Settings", search for "app icon" and replace the value of Asset Catalog App Icon Set Name with the name you used for the App Icon earlier. copyright www.7zhao.net

www.7zhao.net

Build the development target app and you will see it uses another set of App Icon, much easier to differentiate between development and production app now :

欢迎访问www.7zhao.net

Caveats

Using multiple targets is handy for multi server situation, but it is not without caveat / downside. Here are some stuff you might need to pay attention to once you use a multi target strategy.

欢迎访问www.7zhao.net

Targets selection when creating new files

When you create a new file by right clicking on Xcode, notice that in the dialog box there is a "Targets" at the bottom. Remember to choose all of the targets (eg: Development and Production) so that this new file / code will work on these targets. You might get compilation error if you missed the target.

本文来自去找www.7zhao.net

本文来自去找www.7zhao.net

Cocoapods installation on multiple targets

If you are using Cocoapods and want to install the same pods into multiple targets, you can modify the Podfile to this:

copyright www.7zhao.net

# Podfile
 
platform :ios, '11.0'
 
use_frameworks!
 
def app_pods
    pod 'Alamofire'
    pod 'SDWebImage'
end
 
target 'MultiScheme' do
    app_pods
end
 
target 'MultiScheme Development' do
    app_pods
end 欢迎访问www.7zhao.net 

You can read more about this on .

内容来自www.7zhao.net

Different plist for different target

When you create a new target, Xcode will create a new plist file and assign it to the new target automatically. If you want to make adjustments on plist file (eg: App Transport Security etc), you would need to adjust both plist.

欢迎访问www.7zhao.net

You can change the plist in the Build Settings of the target :

本文来自去找www.7zhao.net

Notes

Put down your pitchfork if you have it in your hand, I know some developers saying it is bad practice, I understand the concern that the code you see might not be the exact code that will be compiled due to preprocessing and such. I have used this strategy and worked with other developers who use this strategy as well, it works well if you take care of its caveats . 去找(www.7zhao.net欢迎您

copyright www.7zhao.net


本文原文地址:https://fluffy.es/multischeme/

以上为Use multiple target / scheme for different server environment URL文章的全部内容,若您也有好的文章,欢迎与我们分享! www.7zhao.net

Copyright ©2008-2017去找网版权所有   皖ICP备12002049号-2 皖公网安备 34088102000435号   关于我们|联系我们| 免责声明|友情链接|网站地图|手机版