How To Add Resource Files Into Xcode Project And iOS App

When you develop iOS app, you may need to add some resource files ( such as image file, audio file, data file etc) into it. And when you create iOS app binary file, you can open it in finder by right click the app binary file then click Show Package Contents menu item in the popup menu list like below. Then it will display the iOS app package content in mac finder like the second picture of below two pictures.

Picture 1 : Click Show Package Contents menu item to display the iOS app content files.

right click the ios app then click show package contents menu item in popup menu list

Picture 2 :

When the iOS app package is opened in finder, it will display all it’s content files, you can see the resource files which has been added in it. In this example the added resource files include Assets.car file, example-images-folder-references folder and it’s sub files, iconfinder_app_store_287607.png image file and iconfinder_icon-3-file-app_315108.png image file. This article will tell you how to add them to the iOS Xcode project to create the iOS app package.

ios app package contents with some resource files has been added new

First you should create a Xcode project, then you can add above resource files into it by two ways, through Project navigator or Asset catalog. We will introduce how to use the two methods to add additional resource files into Xcode project as below.

1. Add Files Into Xcode Project.

You can add one or more files ( such as image files ) into Xcode project follow below steps.

  1. Right click the Xcode project folder, then click Add Files to ” project name “ menu item in the popup menu list.
    add files to swift xcode project
  2. Then select one or more files that you want to add to the Xcode project. Check Copy items if needed checkbox in Destination section to copy the added files to the target iOS app product. If not check this checkbox, the file will not be copied into the target iOS app, so when the files are missing outside then the iOS app will not work properly.
  3. Because we are not adding folders, so you can select any radio button in the Added folders section. The Added folders section is only useful when you add folders into Xcode project.
  4. Select one target by check the checkbox before it in Add to targets section. Then click Add button to add it.
    add single image file to xcode project
  5. After click the Add button, you can see the files has been copied into the selected project target’s Build Phases —> Copy Bundle Resources phase in center editor window. Then when you build the iOS app product, it will copy above files into the final iOS app package.
    single image file is involved in target - build phases - copy bundle resources
READ :   How To Set iOS App Supported Devices And Orientation In Xcode Target Or Programmatically

2. Add Folder Into Xcode Project, And Choose Create Groups Radio Button In Added folders Section.

Beside files, you can also add folders into Xcode project follow below steps.

  1. Right click the Xcode project in navigator.
  2. Click Add Files to ” project name “┬ámenu item in the popup menu list.
  3. Select a folder, check Copy items if needed checkbox in Destination section.
  4. In the Added folders section, you can select either Create groups or Create folder references radio button.
  5. In this example, we will select Create groups radio button. This will add a group in Xcode left project navigator pane, the group name is same as the folder name. add folder as group in xcode project
  6. When you click the Add button in above dialog window, it will create a same name group folder ( a yellow folder with name example-images-group ) in Xcode left project navigator panel and add all the files in the selected folder into Xcode project target’s Build Phases —> Copy Bundle Resources phase in center editor. All the files name in the folder is added at the top level of Copy Bundle Resources phase. All these files will be copied into the target iOS app product when build it. Please note the group folder is not added in the target’s Build Phases —> Copy Bundle Resources phase, only the group files are added in it.
    add folder as group in xcode project then the files in the folder will be added at top level of copy bundle resource phase

3. Add Folder Into Xcode Project And Choose Create folder references Radio Button In Added folders Section.

  1. When you add folders into Xcode project, you can also choose Create folder references radio button in the Added folders section.
    add folder into xcode project and select create folder references radio button
  2. Now when you click the Add button in above dialog window, the referenced folder will be added as a same name blue folder ( example-images-folder-references ) in left project navigator pane. And the selected folder and it’s sub folder and files will be added in the Xcode project target’s Build Phases —> Copy Bundle Resources phase. The referenced folder will be added at the top level of Copy Bundle Resources phase. This is different from Create groups radio button choice.
    copy the folder and it's content files into xcode project target build phases ---> copy bundle resources phase
READ :   How To Configure Xcode Default, Workspace, Project Build Directory Location

4. Add Files Set In Xcode Assets Folder.

Beside add files or folders in Xcode project navigator pane, you can also add them in Xcode Assets folder to add them into iOS app product when build it, you can read article How To Add iOS App Icon, Image, Color Set To Xcode Project Assets Catalog Correctly to learn more.

When you build the iOS app product, the Assets.xcassets folder will be copied into the iOS app also, this is because Assets.xcassets folder has been added in the project target’s Build Phases —> Copy Bundle Resources phase when create the Xcode project. The Assets.xcassets folder content files will be compressed into an Assets.car file.

ios app package contents with some resource files has been added new

5. How To Create The iOS App Product To Include All The Added Resources.

After add all above resource files in to the Xcode project, they will be all added in the project target’s Build Phases —> Copy Bundle Resources phase. To copy them into the final iOS app, please follow below steps.

  1. First click Product —> Scheme —> Choose Scheme menu item to select the correct scheme.
    xcode product - scheme - choose scheme menu
  2. Then click Product —> Build menu item at Xcode top menu bar to create the iOS app product.
  3. Then you can find the iOS app in Xcode left navigator pane Products folder. If the iOS app product’s name’s color is red then it means the build is not success. Fix the errors and rebuild it until success.
    xcode project - products - app binary file
  4. If you want to locate the target iOS app product’s binary file in mac finder, just right click the product name, then click Show in Finder menu item in the popup menu list.
    show xcode products app binary in finder
  5. Then it will open a mac finder to locate the iOS app binary file, you can right click it and click Show Package Contents menu item in the popup menu list to display it’s content to verify whether the added files has been copied successfully or not.
    right click the ios app then click show package contents menu item in popup menu list

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.