WkWebView will not load HTMLML string

I am trying to show in a WkWebView an html page that I previously uploaded and saved to String.

This is how I set up my WkWebView:

webView = WKWebView(frame: self.blankView.frame) webView.navigationDelegate = self webView.loadHTMLString(tipShow.htmlString!, baseURL: nil) view.addSubview(webView) 

The html line I'm trying to show is:

  <html> <style type="text/css"> * { -webkit-touch-callout: none; -webkit-user-select: none; /* Disable selection/copy in UIWebView */ } </style> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>TITLE</title> <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css"> <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script> </head> <body> <div data-role="page" id="page1"> <div align=justify style="margin-left:20px; margin-right:20px"> <font size="4.5" face="HelveticaNeue-Light"><br> <p> THIS IS A TEST </p> </div> </div> </body></html> 

When WkWebView shows in my view, it is just as great.

enter image description here

Can someone explain to me why and how to solve this problem?

+9
source share
3 answers

SWIFT

Perhaps you are doing it too hard. It worked for me ...

Import WebKit First

Second, create @IBOutlet under your class:

 @IBOutlet weak var webView: WKWebView! 

Third, put the following in your viewDidLoad ():

 let htmlString:String! = "\(YourString)" webView.loadHTMLString(htmlString, baseURL: NSBundle.mainBundle().bundleURL) 

SWIFT 3 Update

 webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL) 
+11
source

I met the same problem as you! The main problem is the project settings:

Check out the Xcode Project> Target> Features> Application Sandbox .

Make sure you check NetWork βœ… inbound and βœ… outbound connections.

See SreenShot below:

enter image description here

+7
source

This example shows how we can use HTML in WKWebView.

 func someFunction() { let wkWebView = WKWebView() wkWebView.loadHTMLString("<html><body></body></html>", baseURL: nil) wkWebView.navigationDelegate = self as? WKNavigationDelegate } func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { //ready to be processed } 

Remember to extend the class from WKNavigationDelegate

get HTML from UIWebView
get HTML from WKWebView
put HTML in UIWebView

0
source

All Articles