Loading Resources in iOS

Introduction I recently encountered a scenario involving loading local resource files. Retrieving resources from the sandbox is fairly straightforward, but accessing resource files from within the app bundle often results in a WebView failing to load them. So I organized my notes on this topic. Accessing Resources from the Sandbox The main sandbox directories are: - Documents directory: You should write all application data files here. This directory is used to store user data or other information that should be backed up regularly. - AppName.app directory: This is the application bundle directory, containing the app itself. Since the app must be code-signed, you cannot modify the contents of this directory at runtime — doing so may prevent the app from launching. - Library directory: Contains two subdirectories: and . The directory holds the app's preference settings files — you should not create these directly, but use to read and write them. The directory is for app-specific support files needed between launches. - tmp directory: Used to store temporary files that are not needed between launches. Methods for obtaining these directory paths: 1. Get the home directory path: 2. Get the Documents directory path: 3. Get the Caches directory path: 4. Get the tmp directory path: Accessing App Bundle Resources Common pitfalls: 1. When dragging a folder into the project directory, if you select , as shown: The code above will output . You must use the form without to successfully load the resource: The object obtained from represents the project's root directory. When using groups to organize files, resource files still reside in the project's root folder, as shown: 2. When dragging a folder into the project directory, if you select , as shown: In this case, you must include the parameter: Managing Resources with NSBundle Create a new folder named , rename it with a extension (e.g., ), add the required resource files into this bundle, then drag the bundle into your project. (It seems that regardless of which import option you choose, subfolders will always be in mode.) Code: All code from this article can be found on my GitHub at .