This article original address: http://www.appcoda.com/using-xcode-targets/
Before starting this tutorial, let's assume that you have completed the development and testing of your application and are now ready to submit a production release. The problem is that some Web services URLs point to the test server, while the API keys are configured for the test environment. Before submitting your app to Apple for review, you'll need to modify all of these APIs and URLs to fit your production environment. It's normal, isn't it? But, in addition to changing these values back and forth between the development and production environments, is there a better way to manage the development and production versions?
Let's start our tutorial here!
First of all, some of you might want to know why two separate databases and environments are needed for application development.
The reason is that as you continue to build new features or develop applications, you want to make sure that the development version and the existing version are differentiated from one another. The standard software development process is designed to use different environments for different versions of software (in our case, the iphone app). Developing version software typically uses a database that is different from the production version (or other systems, such as analysis). That's why we should use different servers and databases in different environments. Developers typically use virtual images and data during testing. In a test or development environment, some test data, such as "Test Comment", "Argharghargh" and "one more Test comment", are usually used. Obviously, you don't want real users to see such a message. If your application uses an analysis system, you may send thousands of events during the testing phase. Similarly, you do not want to mix test data and production data in the same database. This is why it is always recommended to use a mutually independent development and production environment.
When using two separate environments, your app needs a way to find out which environment it should connect to. A common method is to define a global variable in the main app delegate that initializes the application to development or production mode.
This method requires you to change the global variables each time you switch the environment. Although it is fast and convenient, there are some significant limitations to this approach. First, because we use the same budle ID in the development and production environment, you can't install two app versions on the same device. When you want to test the development version of your app on the same device, it's very inconvenient when you're still using the app's production version. In addition, this approach may accidentally publish the development version of the app to the store. If you're dead. Changing a single global variable, you will publish the wrong application. I remember that one time before the application submitted, forget to change the global variables, the user obtains is the application development version. It was a disaster.
In this article, I'll show you a better way to differentiate between development and production versions.
Specifically, we will create a development target in Xcode. This method applies to both new, and existing large projects, so you can use an existing application to learn this tutorial.
By using this approach, the production and development versions of the app will have the same underlying code, but can have different icons, bundle IDs, and only point to a different database. The publishing and submission process is straightforward. Most importantly, testers and managers can install two versions of the application on the same device, so they know exactly which version is being tested.
How to create a new target
How do I create a development target in Xcode? I will use my own template project "Todo" to step through the development process. You can use your own projects, step by step.
1. Locate the project settings in the Project Navigator panel. In the Tragets area, right-click the existing target and select duplicate to copy the existing target.
2.Xcode will ask you if the new target is being developed for the ipad. For this tutorial, we select "Duplicate only".
Note:if your project supports Universal Devices,xcode will not promt the above message. NOTE: If your project supports generic devices, Xcode does not prompt for the above message.
3. Now we have a new target, and a new build scheme called Todo copy. Let's rename it and make it easy to understand.
• In the Targets list, select the new target. Press ENTER to edit the text and select an appropriate name. I prefer "Todo Dev". You are free to choose any name you like.
• Next, select "Manage Schemes ...", select the new scheme you created in step 1, and press "Enter". Make the Cheme name the same as the target name (that is, the name you select for the new target.) )
4. Step 4 is optional, but it is highly recommended. If you want to make a simple distinction between development and production versions, you should use separate icons and splash screens for each version. This will give your testers a clear idea of which app they are using, preventing you from releasing the development version of the app.
Select Assets.xcassets, and add a new icon. Right-click Icon>app icon & Launch Images > New iOS App Icon. Rename the new icon to "Appicon-dev", and then add your own picture.
5. Now go back to project settings, select your development target, and change the bundle identifier. For example, you can simply add "Dev" to the original ID. If you performed step 4, make sure that you set the app icon to the one created by the previous punch.
6.Xcode will automatically add the Plist file (such as Todo copy-info.plist) to your target and you can find it in the root directory of your project. Rename it from "Copy" to "Dev" and save it under the original plist file. This makes it easy to manage files.
7. Now open "Build Settings" to develop target, scroll to "packaging", change the value to develop plist file (like Todo dev.plist)
8. Finally, we will configure a preprocessing macro/compiler tag for the production and development targets. This allows you to use tags in your code to detect which version of the application is running.
For the OBJECTIVE-C project, select Build Settings and scroll to Apple LLVM 7.0-preprocessing. Expand Preprocessor Macors, add the Debug and Release fields variables. For developing target (such as Todo Dev), set the value to Development = 1. In other words, set the value to Development = zero for the production version.
For swift projects, the compiler no longer supports preprocessor directives. Instead, it uses compile-time properties and build configurations. Add an identity to mark the development version and select develop target. Select build settings and scroll down to the swift Compiler-custom flags section. Set the value to-development to indicate that target is the development version.
Now that you have created and configured to complete the development target, what is the next step?
Using target and macro
By configuring Macro Dev_version, you can use it in your code to perform dynamic compilation for your project. Here is a simple example:
Object-c
#if DEVELOPMENT #define SERVER_URL @"http://dev.server.com/api/" #define API_TOKEN @"DI2023409jf90ew" #else #define SERVER_URL @"http://prod.server.com/api/" #define API_TOKEN @"71a629j0f090232" #endif
In Objective-c, you can use #if
the check DEVELOPMENT
case and set the URLS/API keys accordingly.
Note: Usually, you should put the above code in the app delegate. But it really depends on where you initialize the app's settings.
Now, when you select "Todo Dev" scheme and run this project, you will automatically create the development version and the server configuration settings for the development environment. You can now upload a development version to TestFlight or Hockeyapp for testing by testers and managers.
Then, if you need to create a production version, simply select "Todo" scheme. No code changes are required.
Some tips for managing multiple targets
1. When adding a new file to a project, don't forget to select two targets at the same time, keeping the code in two versions of synchronization.
2. If you are using Cocoapods, do not forget to add the new target to the podfile. You can use Link_with to specify multiple targets. Podfile File Contents:
source ‘https://github.com/CocoaPods/Specs.git‘ platform :ios, ‘7.0‘ workspace ‘todo‘ link_with ‘todo‘, ‘todo Dev‘ pod ‘Mixpanel‘ pod ‘AFNetworking‘
[Hmly] 3. How do I manage development and production versions with Xcode targets?