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?
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 .
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.
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.
This should do the trick:
NSString *myHTML = @"<html><body><h1>Hello, world!</h1></body></html>";
[myUIWebView loadHTMLString:myHTML baseURL:nil];
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
baseURL can be a fileURL to your resources folder if you have any images.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With