Rishi
Rishi

Reputation: 3539

unable to add a local html+css file in iphone webview

I am loading a local html page by this code:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"title" ofType:@"html"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];

...it is loading perfectly. I have added the link tag to load the local css file which is added in the project as well but it does load the page as expected.

<head>
<link media="only screen and (max-device-width: 480px)" href="/style.css" type= "text/css" rel="stylesheet" />
</head>

But when i load the local html page with css from the server it loads perfectly.

<head>
<link href="http://somehitng.com/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
</head>

Can anyone help me what I am doing wrong?

Upvotes: 1

Views: 1650

Answers (1)

Simon Goldeen
Simon Goldeen

Reputation: 9080

You need to set the base URL of the web view to the location where the HTML file is, so local link references work.

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]];

Upvotes: 5

Related Questions