IOS Mobile development:using Xcode Targets to reuse the code uses Xcode Targets

Source: Internet
Author: User
Tags configuration settings

In the context of IOS mobile app development, a clone is simply an app, which is based off another mobile app. A clone usually have more or less the same code base with probably a slightly different functionality. It's quite common for this app to has the new images, even though most of the UI is the same as the original app.

There is different ways to handle the development of clones. Probably the most common or the one, the would first come to mind is using branches.

However, I ' m going to talk on a different approach in the form of targets.

1. Targets versus branching

Using branches is not a bad approach by all means. However, there is some serious drawbacks when using them.

Firstly, need to go your first app to solve bugs the users could have found. Once you ' ve fixed those bugs, you'll want to merge those fixes in your new branch just to see this you'll have a bunch o F conflicts.

By the time you ' ve successfully made the merge, you'll notice that ' ve spend more time here than doing actual work on T The He new app.

Fortunately there is a better to that does not require branching. The most IDE's has some sort of target support (in Android they is called flavors).

Targets work and more like configuration files. You use the same code for all your targets, but each target is configured differently, whether it's a new app icon or a SL ight Change in logic.

The setup can is tricky but it's far more powerful than using branches. Let me show you.

2. Adding a new target

We'll be using the Xcode 5 for this article and we'll be using a project that I called Multitarget.

The first thing you want to do are select the project item in Xcode's Project window which may look a little bit like this:

IOS App Development Xcode Adding a new Target

The first step is to duplicate the target like this:


Duplicate Target Xcode

Change the name of the "copy to whatever" want and keep it selected as we need to configure a few things.

Since This is a new app, you probably want a different bundle ID for each target. You can do this manually by changing the value Bundle identifier in the Info section of the the target, or you can be a bit mo Re flexible by adding a new user defined setting.

This also allows-a different bundle ID for the app built for Debug or Release.

To does this, select the Build Settings section and go to the Editor menu and select Add user-defined Setting like this:


IOS App Development Xcode Using Targets

Give The new user setting a name like app_bundle_id and Give it a value:



Describe the image

And finally tell the info.plist about this setting and in the Info sections change the value of the Bundle identifier to ${ APP_BUNDLE_ID}:



Bundle identifier Xcode

Of course can add more user settings but remember so to use this setting and you need to use the following syntax: ${yo Ur_user_setting}

Note:if your user setting contains spaces you'll need to quote it like so: ${"your_user_setting"}

This was useful if your user setting is a path or some string, may contain spaces.

Now if your go to the general section of the "target" you'll see your change reflected:



Describe the image

The value here comes from the newly defined user setting and if you've defined a different bundle identifier for debug or Release, the Bundle identifier here'll change too. "Automagically!"

Pretty sweet:) Moving on ...

3. Using Asset Cataloguesuse asset catalogues to add different app icons and launch image

New in Xcode 5 is asset cataloguesWhich is treated as special files in Xcode but is in fact, folders on your hard disk with images inside them.

Not only is asset catalogues great for organizing your the image files, but they also the if you is missing @2x version s of your images etc ...

If you used to previous versions of Xcode, you probably has a resources folder with all your images inside them. Also There is probably some other images outside this folder for your icons and launch images.

So-what-you-need to-do-sure that's using the asset catalogues first. This is the ' done ' in the ' General ' section of the target. Above the APP Icon sub section has a button that Reads:use Assets catalogue. Click on the IT and you'll have the something like this:


Assets Catalogue

In your files list on the left of the Project window you ll also has a file called "Images.xcassets". Select it and you'll see the IT opens as a kind of an image organizer:

Describe the image

Note: Click on this xcassets file and then enter this file, you can see there are AppIcon and launchimage two files, click any one will be realistic current picture resources. After you choose to add a new AppIcon, you can rename it,

Then click on the project name to browse all current targets, select the target you want to configure and then find the AppIcon option in the general to select a different file.

1, this must be all the first to load the tablet resources, otherwise the compilation will be wrong to find the appropriate resources

2, this must provide the appropriate size of the picture resources, if not suitable although can browse but compile will report the above error

3, the specific size can view the relevant documents,


The images is from the your first app. Probably want to add the icons and launch images from the new app.

Select the + button at the bottom and select New App Icon. Repeat to make a New Launch Image.


New app icon Xcode

You'll want to rename the app Icon and launchimage to match the new app name. I did it by adding a prefix to the file name:


Launch image

That's the you can pick which icons and launch image of you want to the use for each target. Select them in the????, as shown here:


Select Icons Xcode

Finally, you'll want to use separate asset catalogues for all the images app. In my example project I ' m using 3 asset catalogues:

-one used by both targets for the app icon and launch image (the Images.xcassets file)

-One used by the multitarget target

-One used by the Otherapp target (ie the second app)


Separate asset

The only thing your need to do are tell each of the target which asset catalogue you want to use.

Phases section of the target, under the Copy Bundle Resources subsection:



Build Phase 1 Xcode

Describe the image

As can see from the images above, both targets use the same Images.xcassets file (which holds the app icon and launch Images). But the Multitarget target uses the mt-media.xcassets and the Otherapp target uses the oa-media.xcassets.

At this point you ' ve successfully separated image assets and some configuration settings while using the same code base.

However, just adding a new skin to the new app are probably not the only change you wanted to make. So let's talk about changing the logic of the app.

4. The same but different

You can set up precompiled macros to differentiate between different target types in your code, choose Apple LLVM 5.1-preprocessing in Target's build settings and then fill in the precompiled macro name and then code as follows:

#ifdef T2

NSLog (@ "testtarget2-info.plist");
    
#elif T1
NSLog (@ "testtarget-info.plist");

#endif
If we had used branches instead of targets, we would probably has to solve the merge conflicts at some point. Even in a world with no conflicts @ all, maintaining 2 code bases was not an easy task, even if they was similar.

When using the targets we can also organize our code in such a-to-it would look as if we had 2 similar code bases and St Ill maintain one big code base at the same time.

However, purists might not consider it-be very elegant.

Enter the world of preprocessor macros. Most c-based languages and their compilers support this feature which allows the use one part of the your code depending on t He condition that supply.

For example, suppose you has this method:

-(void) greet {
nslog@ "Welcome to Multitarget");
}
No matter which target you select, the method would always show the same text in the log. This is not the want of course.

So what can we change this method to is different depending on the selected target? You guessed it, with preprocessor macros.

In Xcode to add preprocessor macros in the Build Settings sections of the target. Double Click on the right of the text "Preprocessor Macros" and a pops up'll show up with a list of all your Macros. ADD a new macro for each target. For instance to Multitarget, I added the macro MT and for Otherapp I added the macro OA:


Preprocessor Macros



Now-to-use these-macros, you'll need-to-change the code above like this:

-(void) greet {
#ifdef MT
nslog@ "Welcome to Multitarget");
#elif OA
nslog@ "Welcome to Otherapp");
#endif
}
#ifdef simply means "if defined". In human terms it comes-asking the compiler if the MT or OA macro is available. Since each target has a different macro, one of the 2 macros would always be set (depending on the selected target you used To build the app).

It should be noted that preprocessor macros don ' t need to be words alone, they can be something like:debug=1

However, they is used a little differently. In this format you usually be more interested in the value of the macro rather than knowing if the macro was available at All.

So instead of the using #ifdef you simply use #if like this:

#if DEBUG = = 1
Do something for DEBUG mode
#endif
Please refer to the documentation of the C language for more information on how to use these statements.

5. No more conflicts ...

As can see, the target solution are not so hard to implement and are definitely less messy than if you ' d use branches. The process of merging branches specially can be quite hard and could even leads to data loss.

By just separating your assets and adding some statements to select Code depending on the target is able to maintain Multiple apps at the same time, so to speak.

And if you use the user defined settings and preprocessor macros, you'll find that this approach are far more flexible than bra Nches.

I hope you can find good the use of this tutorial and if you had any questions post them below.

Until Next time!

Jesus De Meyer has a bachelor's degree in multimedia and communication technology from the PIH University in Kortrijk, Bel gium.  He has a years of experience developing software applications for MAC and more than 5 years in IOS mobile app Development. He currently works at Itexico as a IOS 7 mobile App Developer and in him spare time he works on his own Mobile apps.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.