Blazer
Blazer

Reputation: 14277

How can I load an HTML file?

How do I load an HTML file into my app (xcode)? I'm using the following code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]]; 
}

When I start the app, I only see a white page. What's wrong?

.h file:

#import <UIKit/UIKit.h>

@interface poules : UIViewController {
    IBOutlet  UIWebView *webView;
}
@property (nonatomic,retain) UIWebView *webView;
@end

Upvotes: 1

Views: 1568

Answers (3)

chown
chown

Reputation: 52738

Here is a working example from one of my projects. My index.html file is in a folder called Documentation/html in the resources directory. Its important to note that these are "folder references", not groups (hence the blue icon):

enter image description here

then to load it in a webView:

NSString *resourceDir = [[NSBundle mainBundle] resourcePath];
NSArray *pathComponents = [NSArray arrayWithObjects:resourceDir, @"Documentation", @"html", @"index.html", nil];
NSURL *indexUrl = [NSURL fileURLWithPathComponents:pathComponents];
NSURLRequest *req = [NSURLRequest requestWithURL:indexUrl];
[webView loadRequest:req];

Upvotes: 3

Robin
Robin

Reputation: 10011

First thing you can check whether your webView is connected or not.

If it is then, you can break down your code to check what is wrong with request that you are trying to load.

1.. Create a NSString for the file path like this, and check if the path is returned or not.

NSString *urlString = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];

2.. Create a NSURL from the above string like this, and check if the url is correct or nil.

NSURL *url = [NSURL URLFromString:urlString];

3.. And then create the request.

NSURLRequest *request = [NSURLRequest requestFromURL:url];

Upvotes: 0

virushuo
virushuo

Reputation: 660

try loadHTMLString:baseURL: method

    NSString *html=[NSString stringWithContentsOfFile:your_html_path encoding:NSUTF8StringEncoding error:nil];

   [webview loadHTMLString:html baseURL:baseURL];

Upvotes: 0

Related Questions