NSBundle Usage: How to Avoid Hard-Coded Paths in iPhone Coding

For developers working on iPhone applications, understanding the use of NSBundle can significantly streamline your coding process. This tool is essential for accessing files within your main .app bundle without the need to embed fixed paths.

Instead of using a rigid path like “/Application/MyProg.app/foo.png”, you can adopt a more flexible approach by utilizing relative paths. This flexibility allows users to install your application in any directory they choose without issues.

Utilizing an NSBundle object, you can pinpoint the location of your application within the local file system, which simplifies the process of accessing and managing resources.

By calling [NSBundle mainBundle], you retrieve the NSBundle object associated with your application. To locate specific resources, such as the Default.png file at the root of an iPhone or iPod touch application, simply use the method: [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png" inDirectory:@""].


Barbara is a dedicated writer for TUAW, bringing readers the latest insights and updates on all things Apple. With a keen eye for detail and a love for technology, Barbara covers everything from the newest iPhone releases to the latest macOS updates. Her articles are known for their clarity and depth, making complex topics accessible to all. When she’s not writing, Barbara enjoys exploring new features on her Apple Watch and testing out the latest iPad apps.