转载

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

Don't make more versions of your source code. Rather than add more code bases, fix the underlying design problem that is preventing you from running from a single code base. From Extreme Programming Explained

For a app developer, usually we had to modify our app for specific targets and markets, different login paths, alternative functions, various layouts, etc.

It's ok to have a helper function to check locales for us, but what if we had to separate our source code into two apps, just like Evernote and 印象筆記 (Evernote for China market) .

Different bundle ids, backend servers, might even icons!

Build configurations

Create a new Build Configuration set:

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

Preprocessor Macros

In project's build settings , find preprocessor marcros column, you can define different values for each of your configurations. You can define KEY=VALUE or just KEY macros here.

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

In your code, you can implement preprocessor marcos like this:

#ifdef foo     // somethings #else     // some other things #endif 

User-Defined Settings

Go to your project's build settings , select Editor -> Add Build Setting -> Add User-Defined Settings.

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

You will get a User Defined field. For my usage, I create two values named APP_NAME and BUNDLE_ID .

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

In my -info.plist , I changed my Bundle display name and Bundle identifier value to ${APP_NAME} and ${BUNDLE_ID} .

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

App icons

I use Asset Catalog handling my image resources, inside Images.xcassets create a new App icon set.

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

Back to your target build settings , you can find Asset Catalog App Icon Set Name field, specify which App icon set shuld be used for configurations.

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

That's it.

Builds

Xcode

From Scheme editor , select which build configurations you want to apply.

[英] 在同一份 Code Base 使用不同资源切出两份 App(iOS)

Command lines

If you run Continuous integration for your project, you must build your project with xcodebuild or xctool , they both had -configuration flag, specify your build configuration name to it.

正文到此结束
Loading...