How to add new iPhone target - iphone

I'm trying to add a new target to my Xcode project so that I can run the same app, but with subtle difference.
Please can someone guide me through the setup of a new target since it is my first time and I'm not sure how to go about doing it.
In particular, I'm interested how I make the new target run the code in the original app. When I tried creating a new target it just made a new app delegate, and viewController file.
Sorry if this is simple, I'm just quite confused.
EDIT:
Please note that I'm after after instructions based in Xcode 4.

In xcode 4, make sure you're in the folder view where you have the project. Select the blue project button, then next to that you can see Targets, Select the target you have there, right click & select duplicate target.
Now you have two build targets.
To apply subtle differences in your app, make a global C flag. in Build settings there is a paragraph named GCC 4.2 - Language, it has a property named Other C Flags. Add your flag here like so:
-DOTHER_VER
Now in your code you can check for this using:
#ifdef OTHER_VER
// some code.
#else
// the subtle difference.
#endif

After you create your new target from settings of your project, you can create an identifier class to check the target. If you use macros everywhere in your code, it'll not be readable and feasible. You can check this tutorial-blog to learn how to do it, besides you may see some best practices there.

Related

Share code between projects in Xcode

I made a generic app, and from that app, I use different images to make different apps (same app, different images).
My idea until now has been every time I was generating a new app, I copied the code and changed the images (and sometimes some lines of code). But every time I change something in the main app, I have to change it in all the sub-apps.
I've been thinking about using target for this (every time I create a new one, I use a new target with a new folder images linked to it), but I'm not sure about this as I never worked with targets before.
Is this the best approach?
And if it is, how is the best way of doing it? I created a new target inside my project, but it creates a whole new project inside of it, with its owns classes.
For your case, using multiple targets is a good solution. For each target you'll include all sources, and only the images used by that target (images can have the same name, just put them in separate folders).
On Xcode 4, you'd need to follow these steps:
Go to Project settings, right click on your main app target and choose 'Duplicate'. This will already include same sources from the main app.
Unlink all images that you don't need in your new target:
select all images that you want to unlink, and in the utilities panel (usually placed on the right), in the 'File Inspector' section, uncheck your new target
Create a new folder in which you place the images for your new target/app, and when you import them in the project, choose to link only with your new target.
When you duplicate your target, it will also automatically create a new info.plist for your new app. You'll need to change the bundle id, and any other options that you wish.
Another option would be to make the main project as a static library, and in all other projects include the main project, and link the target. Won't get into this, since the targets solution seems better for your case, for only changing the images.
I wish to add a link showing howto - in support of the above answer. Hope it will be helpful to others also.
iOS Mobile Development: Using Xcode Targets to Reuse the Code
http://www.itexico.com/blog/bid/99497/iOS-Mobile-Development-Using-Xcode-Targets-to-Reuse-the-Code

How can I remove ARC from iPhone project build for iOS 5?

Now I have a Xcode project which is built for iOS 5, but now i have to install it on an iPhone 3GS, which has a older iOS. I came to know that this is because I am using ARC in this project and it is not available in iOS 3.2. How can I remove ARC from my current project?
If you want it disabled for the whole project, you need to go to the target's build settings and set it to No
very simple first go to targets in that go "buldphases"-->compileSource--> here "yourfile.m" --> set value
-fno-objc-arc(only particular files )
if u want remove ARC in entire project go to targets in that go "buldsettings" ---> objective-c automaticrefcount :NO
In XCode, there is a global compiler flag called -fobjc-arc. If you disable this (-fno-objc-arc), ARC will be disabled in your whole project. If only a few files don't support ARC, use the -fno-objc-arc flag on those files only. The last flag "wins", see http://clang.llvm.org/docs/AutomaticReferenceCounting.html#general.
However, make sure that you know what you're doing! If the project was designed around ARC, there may not be any deallocation-code in there, including dealloc methods. Also, your properties might be unsupported (strong, weak etc.). You'll most likely end up in a complete mess.
Your best bet would be to just upgrade your 3GS.
Go
Build Settings
make sure "all" option is selected
search for automatic you will find Objective-c Automatic Reference counting set NO

Use Automatic Referencing Count in specific classes in ios

Actually i came to this question when i was trying to add some classes that have been made upon ios prior to IOS 5 and these classes doesn't having ARC and the project i am trying to add is made upon the IOS 5 and it give me the compile time error related to ARC the classes having suck kind of information that if i try to remove the release/retain then it start behaving irregular.That is my problem, Now come to question i want to know that is there any way so that i can mark those classes not to use ARC so that the newly created classes that having base SDK ios5 compile with ARC and i mention not to use ARC simply compiled with their retain/release values.That is the only way i have left i think for making properly this app.
Any idea how i can use those classes that is having base sdk prior to ios5.
Thanks,
the image below will show you how to do it.
-fno-objc-arc flag to disbale arc
-fobjc-arc flag to enable arc
Go to your project settings, under Build Phases > Compile Sources
Select the files you want ARC disabled and add -fno-objc-arc compiler flags. You can set flags for multiple files in one shot by selecting the files then hit Enter key.
Go to Issue Navigator -> Validate project settings -> Perform changes. In this way Xcode automatically remove release and retain keywords from whole project and convert into ARC compatible.

Why should your constant implementation file be added to your frameworks target?

I was reading an old post on here: Constants in Objective-C which talks about how to set up a file for your constants when doing iPhone dev in objective C. One of the responses (which got a lot of helpful votes) says that "Constants.m should be added to your application/framework's target so that it is linked in to the final product."
My question is, how do you do this, and what is accomplished by this? Does this make it so you don't have to import the constants file in the files you need to use it for?
Also, does the same method talked about in the response apply to integer constants, etc?
Thanks.
It makes it so that the constant values are available in the linked binary. Your header file will be included by reference (since you #import it) but the compiler needs to know that you want that .m file to be part of the final binary.
In XCode 4, choose your project from the top of the Project Navigator (the file tree view tab on the left). Select your target from the list of targets that appears in a new column to the right of the Project Navigator. Select the Build Phases tab on the right. Open up the Compile Sources item. Here are all of the files that are being compiled to build your binary. Make sure your constants file is in here. If it is not, find it in the Project Navigator and drag it into the Compile Sources pane.
In Xcode 4, beside the method Seamus mentioned, you can also select that .h file, open right view (at the top-right conner, there're three), in the first tab there's target membership, select the target you want to add it to and you're set.
You have to make sure you #import and also the file has to be in build target. #import make sure your complier know where to look for, include the file in target make sure when complier go and look, there's that file (The file won't be include in your build if it's not part your building target even it's in your project).

Rename a class in Xcode: Refactor... is grayed out (disabled). Why?

Why is Refactor... grayed out (disabled) in Xcode?
I'd like to rename a class.
Select the class's symbol in its header file - i.e. the bit just after #interface. Then the refactoring stuff should be enabled.
If you are using Objective-C++ (i.e. mix Objective-C with C++ code) then refactoring is disabled in xcode since it does not support refactoring of C++ code.
Refactor might also be disabled if affected files (most likely the file with your class in it) are not saved.
I've been using Xcode for 5 years now, and refactoring has never worked correctly (even xcode 4.6 has major bugs where it WILL corrupt your source code!).
The workaround has always been (still works 100%, even in cases where Apple's code fails)
use shift-command-f to find all uses of the file
select "replace" in the search settings
"replace-all"
do the following for the .h file, and REPEAT IT FOR THE .m FILE (if you have one):
right click the original file, and select "show in finder"
delete the file from xcode (select "delete references only" when asked)
rename the .h (and .m if you have one) in Finder
in Xcode, select "Product -> (hold down Alt) -> Clean Build Folder"
quit xcode (you can usually get away with not doing this - but NOTE: there are some other MAJOR bugs in Xcode where it crashes itself if you don't do this)
re-open xcode
drag/drop the .h and .m back into Xcode
wait a few seconds (some of Xcode's core methods are asynchronous - allowing it to corrupt your project)
finally, when it seems to be doing nothing (and your hard disk isn't making any noise any more): cmd-b to re-build
I have a 100% success rate with this method. I just tried refactoring with Apple's "Refactor -> Rename" in latest xcode and it failed - again!
(this time with the incorrect error: "Unable to determine the language of", one of those error messages where Apple put the wrong text in place)
I'm going to my project at finder, then change files name by get info.
After that, at xcode -> Project Navigator I delete the files.
At end, I click right on the class and Add files to ..., and add these files again.
It worked for me.
For me I realized Refactor was disabled because the Xcode project I had opened was referencing a Base SDK that was missing. Edit Project Settings and in the Build tab set the Base SDK to one that you have (like for me this was iOS 4.2). This enabled Refactor for me.
Also, it could happen that you renamed the filename for the class, either outside xcode or by ctrl-clicking the filename and then renaming it.
xcode refuses to refactor if filename does not match with the class name.
Go to your class' header file and find the line that looks similar to this:
#interface YourClassName
Right click on the class symbol (e.g. YourClassName) and you should be able to select Refactor -> Rename...
I just tried this and it works in Xcode 5.
This may be a bit late, but I stumbled across this post because I was unable to refactor my "ViewController.swift" file to "WhateverViewController.swift". I tried selecting the file in the Project Navigator and then selecting "Editor -> Refactor" from the top menu, but 'rename' is always greyed out.
Instead, what worked was selecting the ViewController name from the editor. So if you have:
class ViewController: UIViewController {
// Code here...
}
Highlight the "ViewController" word and then select Refactor from the menu or right-click and select Refactor -> rename.
Hopefully that helps...
Had this problem as well. I ran through trying to find missing SDKs, saving files, and looking for Objective-C++ code as mentioned above, and all it took to fix my machine was rebooting XCode.
Seems a little buggy still.
BTW, this was for XCode 4.0.1
Refactoring works If you first change the file name in the project navigator.