iPhone Coding: Use NSBundle and avoid hard-coded paths
Listen up, iPhone coders, NSBundle is your friend. It lets you look in your main .app bundle and retrieve files without hardcoding paths. Avoid "/Application/MyProg.app/foo.png" and replace your absolute references with relative ones. This lets your users install your applications wherever they wish.
An NSBundle object locates your app in the local file system so you can access resources and use them in your programs. [NSBundle mainBundle] returns the object for your app. When you use the pathForResource: ofType: inDirectory: method, you can easily find the path to those resources. For example, to find the Default.png file in the top level of an iPhone or iPod touch application, use: [[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png" inDirectory:@""]]. Couldn't be simpler.
Skip the type argument (use @"") for resources without extensions.
Share
Categories
Listen up, iPhone coders, NSBundle is your friend. It lets you look in your main .app bundle and retrieve files without hardcoding paths....
Add a Comment
I love it when you talk dirty. [o_O]
November 14 2007 at 11:47 AM Report abuse Permalink rate up rate down ReplySo, I take this post to mean there are actually people out there writing apps for iPhone that don't already know about NSBundle?
That is a scary thought and doesn't bode well for the quality of software being written for iPhone right now.
Erica - would you ever consider a cocoa tutorials site, or one on the iPhone? Seriously. From a programmer of many years somewhat scared by objective c
November 13 2007 at 11:31 PM Report abuse Permalink rate up rate down ReplySlight correction:
[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png" inDirectory:@""]]
should be:
[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png" inDirectory:@""]
Thanks to Erika and indiekiduk. I have been using NSBundles since day 1, seemed way to smart not to. But have now been wondering how to pull files from /private/var/root/ for an add-on I'm going to do for my App. I didn't know of the inDirectory function, but I hadn't dug thru the headers yet.
November 13 2007 at 9:38 PM Report abuse Permalink rate up rate down Reply#1: Whaaa? A politely-written request about a separate iPhone-hacking section?! What site do you think you're on?!
November 13 2007 at 8:11 PM Report abuse Permalink rate up rate down ReplyFor images and UIKit apps there is an easier way:
UIImage* fooImage = [UIImage applicationImageNamed:@"foo.png"];
Erica - you totally need to convince the powers that be that you need a new blog for all of this juicy iphone hacking goodness. Keep up the great posts!
November 13 2007 at 7:26 PM Report abuse Permalink rate up rate down ReplyHot Apps on TUAW
Deals of the Day
more deals- Verizon Leather Sleeve for Tablets for $4 + free shipping
- Wicked Jaw Breaker Noise-Isolating In-Ear Headphones for $6 + free shipping
- Refurb Apple MacBook Air Laptops: 12" 64GB SSD for $699 + free shipping
- JVC Motion Sensing Clock Radio with Dual iPod Docks for $55 + free shipping
- Apple iPhone Headset with Mic for $4 + $2 s&h
- Refurb Apple iPod nano 8GB MP3 Player for $99 + free shipping, 16GB for $119
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



8 Comments