views:

349

answers:

3

i want to load html file from Resources in WebView.

in Resources i have:

test.html

testfolder->test.html

this code works perfectly:

[[webview1 mainFrame] loadRequest:
 [NSURLRequest requestWithURL:
  [NSURL fileURLWithPath:
   [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"]]]];

and this one - crash the app (SIGABRT):

[[webview1 mainFrame] loadRequest:
 [NSURLRequest requestWithURL:
  [NSURL fileURLWithPath:
   [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"  inDirectory:@"testfolder" ]]]];

How can i get files from folders?

+2  A: 

You're going about it the right way, so that means either you've got a typo in your file/directory name or the "testfolder" directory isn't getting included in your application bundle.

Make sure that "testfolder" is in the Resources section of your XCode project as a folder (not a group).

Look at the "Build Results" window when you build your application; you should see steps in there that say "copy test.html" and "copy testfolder".

Note that (when building for the simulator) you can also examine the contents of the application bundle directly -- look in Library/Application Support/iPhone Simulator/User/Applications

David Gelhar
thanks! that help.and hint - the group show in resources with yellow folder icon, the folders with blue folder icon.
cru3l
A: 

Documentation for NSURL fileURLWithPath: says

Parameters

path

The path that the NSURL object will represent. path should be a valid system path. If path begins with a tilde, it must first be expanded with stringByExpandingTildeInPath.

Passing nil for this parameter produces an exception.

and for NSBundle pathForResource:ofType:inDirectory:

Return Value

The full pathname for the resource file or nil if the file could not be located.

Could it be that the file is not in the directory path that you specify?

leson
A: 

Your code looks correct.
Are you sure your folder testfolder is included in your build?

If not, your call to pathForResource: ofType: inDirectory: will return nil. And according to the documentation of the NSURL class, passing nil for the parameter of the fileURLWithPath: class method produces an exception.

To check that your folder is included in your build process, expand the 'Copy Bundle Resources' build phase, and look for your folder. If it is not there, drag and drop it into it.

A way to check that you added it correctly is to navigate to your builded application, right click on it and select the "Show Package Contents" item.

Guillaume