iOS – the correct way to load the HTML file from the resource to the UIWebView?

I think it will be easy to load html files from my resources into UIWebView, but it looks like it needs extra things, and I won’t do it. So far, I’m in my In viewDidLoad in the controller:

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

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

I tried The regular URL (google.com) works well. But in my source file, it just presents a white blank screen.

Two things:

1)

What you have now is not a URL. This is just a file name. You need to enter the radialGradient. Add “file:///” in front of html.

2)

And, you may also need to provide a resolvable path for radialGradient.html. Now your application does not know Where to find it.

It’s like:

NSURL * resourcePathURL = [[NSBundle mainBundle] resourceURL];
if(resourcePathURL)
{
NSURL * urlToLoad = [resourcePathURL URLByAppendingPathComponent: @"radialGradient.html"];
if(urlToLoad)
{
NSURLRequest * req = [NSURLRequest requestWithURL: urlToLoad];
[webView loadRe quest: req];
}
}

I think it will be easy to load html files from my resources into UIWebView, but it seems It requires additional things, and I will not do this. So far, I am in the 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 tried the regular URL (google.com) and it worked well. But in my source file, it just presented a white blank screen.

p>

Two things:

1)

What you have now is not a URL. This is just a file Name. You need to add “file: ///” in front of radialGradient.html.

2)

And, you may also need to provide a resolvable path for radialGradient.html. Now your application doesn’t know where to look for it.

It’s like:

NSURL * resourcePathURL = [[NSBundle mainBundle] resourceURL];< br />if(resourcePathURL)
{
NSURL * urlToLoad = [resourcePathURL URLByAppendingPathComponent: @"radialGradient.html"];
if(urlToLoad)
{
NSURLRequest * req = [NSURLRequest requestWithURL: urlToLoad];
[webView loadRequest: req];
}
}

Leave a Comment

Your email address will not be published.