Skip to Content

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.



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

*0 / 3000 Character Maximum

8 Comments

Filter by:
ymilord

I love it when you talk dirty. [o_O]

November 14 2007 at 11:47 AM Report abuse rate up rate down Reply
gak

So, 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.

November 14 2007 at 10:52 AM Report abuse rate up rate down Reply
Garry Schafer

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 rate up rate down Reply
tragiclos

Slight correction:

[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png" inDirectory:@""]]

should be:

[[NSBundle mainBundle] pathForResource:@"Default" ofType:@"png" inDirectory:@""]

November 13 2007 at 10:39 PM Report abuse rate up rate down Reply
Patrick

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 rate up rate down Reply
Macroy

#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 rate up rate down Reply
indiekiduk

For images and UIKit apps there is an easier way:

UIImage* fooImage = [UIImage applicationImageNamed:@"foo.png"];

November 13 2007 at 8:08 PM Report abuse rate up rate down Reply
Ross

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 rate up rate down Reply
Buy an ad here

Hot Apps on TUAW

Tweets

© 2012 AOL Inc. All Rights Reserved.