How to include support documents in xcode cocoa app? - xcode

In iOS version of app, sample/template files are added to Xcode in a group and then become a directory of that name within the app bundle. Easy enough.
What is the Cocoa equivalent? When I try the same thing, Xcode (Swift 3) fails on build with a "Command /usr/bin/codesign failed with exit code 1". How does one add support files (or directory of files) to a Cocoa app?

In macOS, I guess you mean that, every App creates a Resources Folder inside the NSBundle. Just check with right click "Show Bundle Content".
If you add resources to Xcode, just by dragging the file anywhere in your project navigation, will be asked to copy that file if needed.
I usually create a group with Supporting File, but thats arbitrary, because it has nothing to do with the file structure inside the project folder on disk nor with the product package.
To create groups just right mouse and select group.
This copies the file inside the Xcode project Folder. And if you choose to add target, then the file will be included to that Resource folder.
You can ask for that File with:
let bundle = Bundle.main
let path = bundle.path(forResource: "Test", ofType: "txt")
Edit
if you have to code sign your resources, then ope the copy files menu in Build Phases, add with the plus button your resource and check code sign on copy. This should provide a proper signing for your resource.
Hope it helps!

Related

How to add json file to Xcode project [duplicate]

I'm trying to add some new resource files to a project which was build by another person on another mac. I think that the project has the provision of the previous person.
Using right click->Add Files to "MyProject" doesn't provide the expected result. After I compile the project, the added file is not visible in the application.
How can I add new resources in my project?
PS: I'm trying to learn Objective-C in a macincloud.com account.
It might be that the file you added was not added to the project build. Click your project > App target > Build Phases
And check that the file exists in Compile sources (if it needs to be compiled), otherwise check Copy bundle resources. If the file does not exist there, drag it there and it should be fine, (from what I understand of your question).
EDIT
You can also check wether a file is added to the target by clicking the file and opening the "File Inspector" (View > Utilities > Show File Inspector, or ⌥ + ⌘ + 1), and check the Target Membership section.
Example:
Adding to Copy bundle resources worked for me
Just drag the resource file (from a finder window) into your project files area (left side) and drop in supporting files.
Once you drop, a dialog will prompt you which targets to add it for. Select all that are appropriate. Thats it.
Just had a similar experience with Xcode Version 11.7 (11E801a), which I mention as it may help others from wasting a day and a half.
In addition to the other answers explaining how to add the resources:
For me the problem was I had my resources listed in Developer Assets under the target's General tab, but also in the Build Phases/Copy Bundle Resources. This worked fine running through Xcode in simulators and even on devices, but when I came to Archive the app, the resources were not being copied. Deleting my resources from the Developer Assets list, but keeping them in Build Phases worked.
And in the end it turns out I only need the resources in the Build Phases section for either running through XCode or Archiving !

Couldn't load a Xcode project with pods [duplicate]

How do I rename a project in Xcode 5?
What steps do I need to take?
In the past this was always a very tricky manual process.
Well, the answer is very very very Apple simple in Xcode 5!
In the Project Navigator on the left side, click 2 x slowly and the Project file name will be editable.
Type the new name.
A sheet will appear with a warning and will list all the items Xcode 5 believes it should change.
You can probably trust it, but you should inspect it.
The list will include the info.plist and various files, but also all the relevant strings from nib/xib files like MainMenu menus.
Accept the changes and you will get the prompt to save a snapshot of the project.
Always make a snapshot when Xcode asks, it will be useful to restore if something does not work.
Change the project name:-
Click on the target in xcode, on the right in "Identify and Type" under name change the name and press the ENTER button on your keyboard.
A window will appear confirming the change and what it will change. Once you confirm it will make the changes.
Change the root folder name:-
Go to the project directory and rename the root folder,
Open the project and u will find all the file are missing, u need to add all the files of project again
Right click the project bundle .xcodeproj file and select “Show Package Contents” from the context menu. Open the .pbxproj file with any text editor.
4>Search and replace any occurrence of the original folder name with the new folder name.
5>Save the file.
Change the Scheme name:-
rename .xscheme file
If your Project is static framework then make sure your header file has public target membership
I really recommend just opening the folder in a general editor such as Sublime Text, and doing a find/replace across the whole folder. The other methods I found were unstable, particularly when combined with .xcworkspace and cocoapods.
In Xcode 8.0, to rename your project, just go through the following instructions as described in Xcode help:
1- Select your project in the project navigator.
2- In the Identity and Type section of the File inspector, enter a new
name into the Name field.
3- Press Return.
A dialog is displayed, listing the items in your project that can be
renamed. The dialog includes a preview of how the items will appear
after the change.
4- To selectively rename items, disable the checkboxes for any items
you don’t want to rename. To rename only your app, leave the app
selected and deselect all other items.
5- Click Rename.
Source: http://help.apple.com/xcode/mac/8.0/#/dev3db3afe4f
Xcode 6 (beta 6 as of now) seems to be not very reliable with renaming projects. For me it didn't rename several of the files and groups. It also doesn't rename the physical folder the project is in. To rename my project to be sure that everything is clean I went the extra length to create a new project with the new name and copy over all the files. The assets are easy to copy but groups have to be recreated. The biggest issue with this however are CoreData data model files. Trying to simply copy this will result in a corrupt model file, even though everything looks like it is alright.
When you re-name the project name in XCode5 then info.plist entry removed from Targets --- > General ---> identity. You just need to mention it again.
In Xcode 7, renaming a project can still break your app. Make sure you backed it up before trying it.
Click on the project icon and find the project name in the inspector pane. If you change it there, Xcode will ask you if you want to rename related files. Might work. But if not, try this brute force approach:
Close Xcode
Using an advanced text editor like Sublime Text or Atom, open the
root folder. It will open the folder structure.
Perform a Global Search and Replace (it's probably cmd + shift + f), and
replace My Wrong App Name with New App. If your project name contained spaces, also search for My_Wrong_App_Name and replace
with
New_App. This changes all file contents.
Now you need to find all
the files inside the project with your old app name. Rename them
all, also the folders.
Important: Open the project file with
right click > Show Package Contents, and rename all files in there.
Reopen your Xcode project or workspace. Compile.
If you use Pods, you need to open the pods project as well and change the files in there.
Here is another great example which works well with xcode 5

Adding resource files to xcode

I'm trying to add some new resource files to a project which was build by another person on another mac. I think that the project has the provision of the previous person.
Using right click->Add Files to "MyProject" doesn't provide the expected result. After I compile the project, the added file is not visible in the application.
How can I add new resources in my project?
PS: I'm trying to learn Objective-C in a macincloud.com account.
It might be that the file you added was not added to the project build. Click your project > App target > Build Phases
And check that the file exists in Compile sources (if it needs to be compiled), otherwise check Copy bundle resources. If the file does not exist there, drag it there and it should be fine, (from what I understand of your question).
EDIT
You can also check wether a file is added to the target by clicking the file and opening the "File Inspector" (View > Utilities > Show File Inspector, or ⌥ + ⌘ + 1), and check the Target Membership section.
Example:
Adding to Copy bundle resources worked for me
Just drag the resource file (from a finder window) into your project files area (left side) and drop in supporting files.
Once you drop, a dialog will prompt you which targets to add it for. Select all that are appropriate. Thats it.
Just had a similar experience with Xcode Version 11.7 (11E801a), which I mention as it may help others from wasting a day and a half.
In addition to the other answers explaining how to add the resources:
For me the problem was I had my resources listed in Developer Assets under the target's General tab, but also in the Build Phases/Copy Bundle Resources. This worked fine running through Xcode in simulators and even on devices, but when I came to Archive the app, the resources were not being copied. Deleting my resources from the Developer Assets list, but keeping them in Build Phases worked.
And in the end it turns out I only need the resources in the Build Phases section for either running through XCode or Archiving !

Cannot add an icon to Resources

Have been inputting and testing some sample code and everything seems to be ok. Just that suddenly I am running into this very frustrating problem of trying to add an icon to the project. Have downloaded an icon file (extension : icns) to my desktop and dragged it to the "Resources" group in xCode. Then a dialogue comes up from xCode where I checked the 2 options : "Copy items into destination group's folder (if needed)" and "Recursively create groups for any added folders". But when I press the "Add" button, an "Alert" message box would come up. It says "Could not copy the icon to ...". Hope that someone knowledgable in this area would give me some hints...
This failure message usually results from attempting to add a file (that is outside the project folder), when a file with that name already exists in the target location within the project folder.
For example, let's say your project folder is ~/Developer/CoolProject/, and the Xcode project is at ~/Developer/CoolProject/CoolProject.xcodeproj. If you drag an image, coolImage.png, from your Desktop to Xcode with the options set as you described, Xcode will copy the image file to ~/Developer/CoolProject/coolImage.png. If you then repeat this same procedure, it will fail the second time with that error message.
What I would do is, in the Finder, check inside your project folder to see if the icns file has already been copied into the project folder. If there's a copy of the icns file already in the project folder, and that icns file isn't listed in the Groups & Files list in Xcode, you can resolve the problem by dragging the existing copy (that's inside the project folder) into the Xcode project window to bring up the Add File dialog. You can keep the same options as before, and this time, Xcode won't need to copy the image and so you'll avoid the error.
[UPDATED] OK, so what you'll want to do, based on the comment above, is go in the Finder and remove the existing .icns file from your project (in other words, move it to the Trash). Then you can re-add an icon with the same name (from a location outside your project folder) without getting the error. Also, in the future, remember that you might want to consider using the Also Move to Trash option when deleting files from within Xcode, if you no longer want the unused items inside your project folder.
Also, you may want to double-check to make sure the file in question is included as part of the Active Target. For example, if you add a file to your project, but the "include in active target" checkbox for the file is unchecked (like the ReadMe.rtfd file in the image below), then the file won't be copied into your application bundle at build time or be available at runtime.
(To add that Active Target checkbox column to the Groups & Files list, Control-click on the table column header view and choose Target Membership from the menu).

Xcode: Multiple targets in same bundle

I have an Xcode project with a Cocoa application target and a shell tool target. These projects share a Core Data persistent store and thus I am hoping to have both projects use the same *.mom file.
To allow this and to simplify deployment I would like the terminal app to to placed inside the Cocoa app bundle and then have both executables access the same *.mom file from inside the bundle.
Is it possible to setup the Xcode project so that the terminal app gets placed inside the Cocoa app bundle and then have the terminal app load the main bundle of the Cocoa app to get the path of this shared *.mom ?
You should be able to do what you want.
Make the Cocoa app target dependent on the terminal app target: click on the Cocoa target, Get Info, General Tab, and click the "+" under the Direct Dependencies pane, and select the terminal target.
Add a "Copy Files" build phase to your Cocoa target: right-click on the target, Add -> New Build Phase -> New Copy Files Build Phase. You'll get an information window where you can pick the destination of the copy; I recommend "Resources", the default.
Drag the terminal app build product (from the "Products" folder in the project navigation pane) onto the Copy Files build phase you just created. This will cause the terminal app to be copied into the Cocoa app's resource folder, which is where your .mom file lives by default, at build time.
In your Cocoa code, you can find the path or URL to the terminal app by querying the main bundle for the path to the resource with the terminal app's name and extension. Your terminal app will need to discover the directory that it is launched from to find the .mom file in the same directory.
Hope this is helpful.

Resources