iOS Load HTML string


Example

Web views are useful to load locally generated HTML strings.

NSString *html = @"<!DOCTYPE html><html><body>Hello World</body></html>";
[webView loadHTMLString:html baseURL:nil];

Swift

  let htmlString = "<h1>My First Heading</h1><p>My first paragraph.</p>"
  webView.loadHTMLString(htmlString, baseURL: nil)

A local base URL may be specified. This is useful to reference images, stylesheets or scripts from the app bundle:

NSString *html = @"<!DOCTYPE html><html><head><link href='style.css' rel='stylesheet' type='text/css'></head><body>Hello World</body></html>";
[self loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

In this case, style.css is loaded locally from the app's resource directory. Of course it's also possible to specify a remote URL.