Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert HTML into a UIWebView

I have HTML Content which was being displayed in a UITextView. The next iteration of my app is display the HTML contents into a UIWebView. So, I basically replaced my UITextView with UIWebView. However I could not figure out how to insert my HTML snippet into the view. It seems to need a URLRequest which I do not want. I have already stored the HTML content in memory and want to load and display it from memory.

Any ideas how I should proceed?

like image 534
Mohan Gulati Avatar asked Apr 17 '10 23:04

Mohan Gulati


People also ask

How do I import HTML into WKWebView?

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 .

How do I display HTML content in Swift?

Show activity on this post. extension String { var htmlToAttributedString: NSAttributedString? { guard let data = self. data(using: . utf8) else { return nil } do { return try NSAttributedString(data: data, options: [ NSAttributedString.

Which method is used to load the content into a UIWebView?

Use the loadHTMLString(_:baseURL:) method to begin loading local HTML files or the loadRequest(_:) method to begin loading web content. Use the stopLoading() method to stop loading, and the isLoading property to find out if a web view is in the process of loading.


2 Answers

This should do the trick:

NSString *myHTML = @"<html><body><h1>Hello, world!</h1></body></html>";
[myUIWebView loadHTMLString:myHTML baseURL:nil];
like image 103
igul222 Avatar answered Oct 05 '22 11:10

igul222


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

baseURL can be a fileURL to your resources folder if you have any images.

like image 42
drawnonward Avatar answered Oct 05 '22 13:10

drawnonward