Share code between projects in Xcode - iphone

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

Related

How to change location of iPhone app icon?

Okay, I am a noob. It must be simple but I stuck here.
I do rigth-click and choose 'select file'.
After I choose the file the dialogue appears:
I want to keep my icons not in the root directory. But I can't figure out how to do this. I tried different ways, even create project from scratch...
Please help.
When you bring image assets into Xcode they will all be in your application bundle's root directory at run time.
It appears that you've added a file named "icon#2x.png" to your project at one time already. Go to the project's summary page in Xcode click Build Phases and then expand the section titled Copy Bundle Resources. There you'll find the reference to the resources that will be copied to your bundle at run time.
More on bundle structures here:
https://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html
Anyway, the way I've done this in the past is to:
Remove the existing image duplicate from Xcode and from the folder structure in Finder.
Add a new folder in Finder call it "/AppIcons" (or whatever).
Copy all the app icons to that folder in Finder.
Drag the "/AppIcon" folder into your XCode project. I typically put images in an "/Images" subdirectory in the Xcode project. Again you can put it wherever you'd like.
In Xcode on the project summary panel, add your app icons from the folder "/AppIcons" by dragging / dropping or by right clicking and adding them as separate files.
Alternatively in the past when I've had problems with this sometimes Xcode will copy the app icons to the root project directory. If that happens, I make sure that I have the "/AppIcons" folder structure on disc (in Finder) and in my Xcode project. I add the app icons to the project, then move them to /AppIcons in Finder, and re-reference them all over again.
Your Copy Bundle Resources should then have the proper file reference. Note that my apps icons go in "/Images/AppIcons"
Good luck!
I would add the image files to your project with the name icon#2x.png. To do this right click where you want to add the files in xcode and click add file/s. Then search for your image and add them using the dialogue that pops up. - there is a check box so that you can either copy them to the projects destination folder which will copy them into you project folder or leave it unchecked (inadvisable) which would reference it's current location on your HDD. Make sure the file doesn't already exist in the project before doing this!
EDIT:
Sorry I forgot to mention then drag and drop the file from within xcode onto the image location box.
In XCode 4 (I'm using 4.6.2) you will get this confusing message if you've just moved the target's info.plist file, but not corrected the location at the top of the summary panel. Fix this and the icons will reappear, assuming they are added to the project, or if not you should now be able to drag them in without xcode insisting on making extra copies.
This tends to happen when you're starting a project and sorting out the mess of default file locations that xcode gives you. I like my project structure to be 99% the same as the file system structure.
This is an easy fix. If the image you want as your App Icon resides outside of the root of your project, when you get that dialog just click "Yes", and then click delete on the newly created file in the root, and "Move to Trash".
The reference is still there and works without a hitch, since the file will still be in the root at runtime

creating a new project in xcode, different options than I expected

I have downloaded the latest varsion of Xcode. All the tutorials i see for creating a new ios application project, mention to create a view-based application.
But I do not see that option. Therefore, whatever I choose, next I do not see the folders mentioned later in the examples. (for example I see no resources folder in my project)
The picture I see when to create a new project is this:
What to do?
"Single View Application" is the same as the good old "view-based application"
In new X-code you won't find window based application option. If you want then select
Empty Application option. Also you will not find MainWindow.xib here. You have to do coding in Appdelegate file to load initial viewController.
If you select Single view Application as Antonio said you will have all viewControllers in a Storyboard. Which is introduced in new iOS 5.
You can create new folder and name it to resources.

Can't drag folder from one project to another in xcode4

I am trying to use facebook connect and you sare supposed to be able to drag and drop folders but it doesn't work in XCode 4! any ideas?
Update 1/8/2017: This is still true in Xcode 8.2.1
Previously:
drag folder from project A to project B.
Now:
1. in project A, right click - show in finder
2. finder - go up a level
3. drag the folder into project B
4. delete unnecessary files inside the newly added folder
Good job, Apple.
PS: And it doesn't work for groups, as #zeeple mentioned in comments.
Dragging resources between projects in XCode is generally a bad idea in my opinion, even if it is supported. When you drag and drop something in XCode, it doesn't actually create a new copy of that thing, it just adds a new reference to its old location.
This can be very bad, because now changing the resource in one project will make the same changes appear in the other. The vast majority of the time this is not what you want. SCM further complicates things, because if you add a resource to Project B by dragging from Project A and then check in the changes and then some other developer checks out just Project B, they will be missing the resources that you "added" to Project B.
So instead of dragging and dropping in XCode, I suggest using the Finder or the command line to explicitly copy your resources into the destination project's source tree. Then in XCode just add the copied files to the project the same way you normally would.

Can an Xcode project with multiple targets have different Settings.bundle's for each target?

I have a settings bundle in an iPhone app which has several subtle variant targets.
The problem is I need to customise some strings in each settings bundle to make them appropriate for each target. It appears that if the Settings bundle is named anything other than Settings.bundle it won't pick up the bundle (understandable, can I specify in a plist somewhere the alternate name?).
If I try and put them in different folders and call them the same thing Xcode gets horribly confused and lets you edit "2 files" that are actually the same single source.
I see no problem. For every target, create a specific folder with files for this target. Now create a group in Xcode for every of those folders and drag each of the new Settings.bundle directories to the corresponding group. When adding the bundles to the project, make sure that every bundle is only built (included) for its target by checking the appropriate box.
AFter you do what MrMage said which is correct.. i had an issue where the Root.plist would not show up under the Settings.Bundle for a specific target.
You have to let xcode know what the setting.bundle file is... click on the settings.bundle that wont show the Root.plist, then go to file inspector
Alter the file Type drop down to be applicationBundle ... now the Root.plist shows up.

Add image to targets folder by code

i want to add a image to the iphone application Targets-Copy Bundle Resource folder by using code,can make it?any suggestion
Do you want to add images to your built application, so they're available at runtime? In this case, just drag them to your Project tree and make sure your target is checked in the Add dialog. If they're already in your project, but aren't currently in the Copy Bundle Resources, you can drag them there from the file tree.