0

This is my code:

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSString *indexPath = [NSBundle pathForResource:@"iHelp" ofType:@"html" inDirectory:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
}

The app is crashing because 'nil' for directory name is invalid (DUH!). So for an imbedded html file, what should go there?

UPDATE: found that "webView" has no outlet, although one is defined in the .h file... will close this because I believe there is an answer here. Will open a new question re: the outlets... thanks everybody!

3 Answers 3

0

If the file iHelp is in the main bundle, you can retrieve its path like this:

NSString *indexPath = [[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

You can also do that like the following:

NSURL *indexURL = [[NSBundle mainBundle] URLForResource:@"iHelp" withExtension:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:indexURL]];
6
  • 1st causes crash; 2nd one no crash, but get a white screen, no html displayed. Mar 16, 2012 at 15:46
  • 1
    What is the error you get? I think it must be that the file itself does not exist. How and where did you put the file?
    – sch
    Mar 16, 2012 at 15:47
  • 1
    @spokane-dude - Both methods work. I've tested them. This must be another problem like the file not being bundled with the app.
    – sch
    Mar 16, 2012 at 15:52
  • It's not the file (it's there)... there is a problem with the outlets. When I try to connect the uiwebview to the "files owner" (I'm using XCode4 and Storyboard, therefore there is no "file's owner" anymore), it doesn't show "webView" only "view". "webview" is declared as IOBoutlet, but for some reason it isn't seeing it. Want me to open a new question? Mar 16, 2012 at 15:56
  • What error do you get with the first method? If it is Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter' Then the file is not there.
    – sch
    Mar 16, 2012 at 16:03
0

Drop the inDirectory: parameter as it will default to your current bundle. Note: it's an instance method.

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSBundle *bundle = [NSBundle mainBundle];
    NSString *indexPath = [bundle pathForResource:@"iHelp" ofType:@"html"];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];
1
  • No it won't. The class method pathForResource:ofType: does not even exist. And the code will not compile.
    – sch
    Mar 16, 2012 at 15:38
0

If it isn't inside a folder reference, just omit that argument:

[[NSBundle mainBundle] pathForResource:@"iHelp" ofType:@"html"];

You can even go straight to URL:

[[NSBundle mainBundle] URLForResource:@"iHelp" withExtension:@"html"];
2
  • 1st error: No known class method for selector 'pathForResource:ofType:' 2nd error: No known class method for selector 'URLForResource:withExtension:' Mar 16, 2012 at 15:41
  • sorry, my mistake should be [NSBundle mainBundle] instead of just NSBundle]
    – borrrden
    Mar 16, 2012 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.