1

I have a super simple webpage that I just want to load a single local file. The file is /Resources/About.html

Here is my code:

NSString *urlPath = [[NSBundle mainBundle] URLForResource:@"About" withExtension:@"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];

What's wrong?

3 Answers 3

3

The method URLForResource:withExtension returns an NSURL, not an NSString. You can use the result of that directly in your request.

NSURL *URL = [[NSBundle mainBundle] URLForResource:@"About" withExtension:@"html"];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
Sign up to request clarification or add additional context in comments.

2 Comments

Yours is working the best, but I am getting this error now: Error Domain=WebKitErrorDomain Code=101 "The operation couldn’t be completed. (WebKitErrorDomain error 101.)"
0
NSString * urlPath = [[NSBundle mainBundle] pathForResource:@"About" ofType:@"html"];
NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
[webView content baseURL:[NSURL urlPath]];

Comments

0

I bet URLForResource is returning nil (Can you confirm that it is not?). It will not perform a recursive search, and will fail if your "Resources" folder is an actual folder (blue in XCode) as opposed to a group (yellow in Xcode). Try using the main bundle's resourceURL and appending your path to the end of it.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.