Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-16-2009, 01:06 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 83
Default NSBundle pathForResource inDirectory gives nil

Hi,
I've been struggling with this for a while, wondering if someone knows how to get a path for a resource that is in a subdirectory?

If my file "w1.geo" is added directly under the Xcode project's "Resources" tree, the following returns a valid path:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"w1" ofType:@"geo" inDirectory:nil];

Now I wanted to change that, so w1.geo is under a folder called "part1". I created "part1" folder using Finder under my Xcode project's main folder, and added the entire folder to the Resources tree by right-clicking "Resources"->add existing files->selecting "part1". I can now see under Resources a "part1" folder, and in it I see w1.geo.

However, now the following returns nil:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"w1" ofType:@"geo" inDirectory:@"part1"];

Does anyone have any idea why? According to the docs, the instance method of pathForResource should have found this directory correctly under the "non-localized" bundle.

tnx
sagimann is offline   Reply With Quote
Old 10-16-2009, 02:24 PM   #2 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Leeds, England
Posts: 109
Default

Creating a "Group" in XCode doesn't create a real Directory within your app Bundle.

You need to import the directory into your app using the following method:

Create a Directory on your Mac.

Select to Add Existing Files to your project

Select the Directory you want to import

In the pop-up window make sure you select "Copy items into destination group's folder" and "Create Folder References for any added folders"

Hit "Add"

Your done!

The Directory should appear blue instead of yellow.

Now try your code:

Code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"w1" ofType:@"geo" inDirectory:@"part1"];
Hope that helps.
BenODwyer is offline   Reply With Quote
Old 10-18-2009, 06:33 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 83
Default Didn't work

Hi,
The proposal did not work. This is what I did:
1. removed the existing group and file references from the Resources tree.
2. created a new directory under my xcode project directory called "part1"
3. right click Resources -> add existing files -> select "part1" dir -> select "Copy items into destination group's folder" and "Create Folder References for any added folders" -> got error message saying that the directory "part1" cannot be copied onto itself

4. So I moved the "part1" dir to some other dir outside the xcode project dir, and repeated the process. This time, the "part1" dir was imported successfully, so I can see Resources / part1 / w1.geo in Xcode.

5. But the pathForResource method still returns nil after I save & build & go.
sagimann is offline   Reply With Quote
Old 10-18-2009, 06:40 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: Leeds, England
Posts: 109
Default

Are you using the same code as before referencing inDirectory:@"part1"?

Tested it at my end and it works great.
BenODwyer is offline   Reply With Quote
Old 10-19-2009, 03:59 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 83
Default

I'm using exactly the same code as I wrote earlier, I also tried to clean & rebuild in case the resources need to be regenerated or sth. I assume the problem is not in the code, but somewhere else... but I can't seem to resolve this :-( I will try to create a new sample project and reproduce the issue. If I succeed, I'll upload it here.
tnx
sagimann is offline   Reply With Quote
Old 09-27-2010, 12:10 AM   #6 (permalink)
Banned
 
Join Date: Jan 2009
Posts: 69
Default

If you read the docs carefully you will see that inDirectory is actually used to load resources from another app bundle.

Thing is: you can just put your resources in subdirectories, but you do not specify this when you get the path. Just use something like:

[[NSBundle mainBundle] pathForResource:@"w1" ofType:@"geo"];

Even though it's in a subdir. The bundle will find this out for itself.

Of course, make sure that all your resources are uniquely named, otherwise you might not know which file you're actually loading.
mesohorny is offline   Reply With Quote
Old 04-11-2011, 02:45 PM   #7 (permalink)
Registered Member
 
Join Date: Sep 2008
Posts: 14
Default

try this code :

Code:
// FileName = your file , Web = your directory >> could be nil

	[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@“FileName” ofType:@"html" inDirectory:@“Web”]]]];
	[webView release];
good luck
bodalal is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 418
12 members and 406 guests
AppleSteve, banatary70, chiataytuday, DaveDee, Domele, ilmman, iOSguru, Jameswhitfield, johnRambo, maryrobertson, Objective Zero, Oral B
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 157,854
Threads: 88,914
Posts: 379,297
Top Poster: BrianSlick (7,072)
Welcome to our newest member, Jameswhitfield
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:33 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0