Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load a local HTML file instead of a web page in Xcode webview?

Hey! How can I load a local html file saved to the project instead of a webpage in this code:

- (void)loadAboutHTML {
UIWebView *aboutHTML = [[UIWebView alloc] init];
NSURL *webURL = [NSURL URLWithString:@"http://apple.com"];
NSURLRequest *webURLRequest = [NSURLRequest requestWithURL:webURL];
[aboutHTML loadRequest:webURLRequest];
[aboutHTML setScalesPageToFit:YES];
[aboutHTML setFrame:CGRectMake(0, 0, 320, 416)];
[self addSubview:aboutHTML];}
like image 968
Peter V Avatar asked May 09 '11 11:05

Peter V


People also ask

How do I display HTML data in swift WebView?

How to load a HTML string into a WKWebView or UIWebView: loadHTMLString() If you want to load resources from a particular place, such as JavaScript and CSS files, you can set the baseURL parameter to any URL .


3 Answers

UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] 
                            pathForResource:@"help" ofType:@"html"]isDirectory:NO]]];
web.backgroundColor = [UIColor clearColor];

thats wat i had used

like image 148
Virat Naithani Avatar answered Oct 19 '22 17:10

Virat Naithani


OPTION 1

Use this

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

Read the contents of the file in your project into an NSString. Then use the above method to load the html content

Use

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

to obtain the string from the file and then use

[webView loadHTMLString:urHTMLString baseURL:baseURL];

OPTION 2

NSURLRequest *urlReq = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]];
[webView loadRequest:urlReq];

UPDATE

- (void)loadAboutHTML {
UIWebView *aboutHTML = [[UIWebView alloc] init];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"html"]]];
[aboutHTML loadRequest:urlRequest;
[self addSubview:aboutHTML];
}
like image 5
visakh7 Avatar answered Oct 19 '22 18:10

visakh7


local web html

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]                                                                           pathForResource:@"file_name_html" ofType:@"html"] isDirectory:NO]]];

http web

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
like image 2
MrTurki Avatar answered Oct 19 '22 17:10

MrTurki