Mohan Gulati
Mohan Gulati

Reputation: 28209

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?

Upvotes: 28

Views: 36996

Answers (3)

yoAlex5
yoAlex5

Reputation: 34175

This example shows how we can se HTML into UIWebView and

func someFunction() {

    uiWebView.loadHTMLString("<html><body></body></html>", baseURL: nil)
    uiWebView.delegate = self as? UIWebViewDelegate
}

func webViewDidFinishLoad(_ webView: UIWebView) {
    //ready to be processed
}

Do not forget to extend a class from UIWebViewDelegate and nil the delegate

Upvotes: 0

igul222
igul222

Reputation: 8637

This should do the trick:

NSString *myHTML = @"<html><body><h1>Hello, world!</h1></body></html>";
[myUIWebView loadHTMLString:myHTML baseURL:nil];

Upvotes: 93

drawnonward
drawnonward

Reputation: 53659

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

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

Upvotes: 6

Related Questions