3

I thought it would be easy to load in a html file from my resources into a UIWebView, but it looks like it requires something extra that I'm not doing. So far I have in my viewDidLoad in my controller:

NSURL *url = [NSURL URLWithString:@"radialGradient.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[webView loadRequest:req];

webView.frame = CGRectMake(0, 0, 768, 1024);    

I've tried regular web addresses (google.com) and it works fine. But with the file in my sources it just renders a white blank screen.

1 Answer 1

7

Two things:

1)

What you have now is not a URL. It's just a filename. You need to be prepending a "file:///" in front of that radialGradient.html.

2)

And, you'll likely also need to put in a resolvable path to radialGradient.html Right now your app doesn't know where to look for it.

Something like:

NSURL * resourcePathURL = [[NSBundle mainBundle] resourceURL];
if(resourcePathURL)
{
    NSURL * urlToLoad = [resourcePathURL URLByAppendingPathComponent: @"radialGradient.html"];
    if(urlToLoad)
    {
        NSURLRequest * req = [NSURLRequest requestWithURL: urlToLoad];
        [webView loadRequest: req];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. I tried adding "file:///" or "file://" and "file://Resources" with no luck. (whole path is @"file://Resources/radialGradient.html")
I was trying to nudge you along the path to a solution instead of just handing it all over :-) I have changed my answer to include source code you can use.
Okay thanks! I'll read this through and see why it is needed. Thanks again

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.