WkWebView won't loadHTMLstring
Asked Answered
E

4

18

I'm trying to show in a WkWebView an html page that I previously downloaded and stored in a String.

This is how I setup my WkWebView:

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

The html string 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 the WkWebView shows in my View it stands like this forever.

enter image description here

Someone can explain me why and how to solve this problem?

Echevarria answered 3/5, 2016 at 20:30 Comment(0)
A
21

SWIFT

You might be making it a bit too complicated. This worked for me ...

First, import WebKit

Second, create an @IBOutlet under your class:

@IBOutlet weak var webView: WKWebView!

Third, place the following in your viewDidLoad():

let htmlString:String! = "\(YourString)"
webView.loadHTMLString(htmlString, baseURL: webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL))
Alexaalexander answered 3/5, 2016 at 21:36 Comment(2)
I was missing the proper baseURLEchevarria
Can anyone provide Objective C version of this ?Borrowing
P
17

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

Check your Xcode Project > Target > Capabilities > App Sandbox.

Make sure you've CHECKED the NetWork ✅ Incoming & ✅ Outgoing connections.

See the SreenShot Below:

enter image description here

Paroxysm answered 14/8, 2017 at 14:26 Comment(3)
This is not iOS, is it?Aden
Re shallow:This case is for OSX WKWebView, But i guess in iOS will be similarParoxysm
that fixes it... I only needed outgoing, but why do I need to have that capability to load a string I generated in the app and load using loadHTMLString()...Carrnan
P
1

I know it's a bit too late for that, but here's the requested Objective-C version:

@property (weak, nonatomic) IBOutlet WKWebView *webView;
[self.webView loadHTMLString:strTemplateHTML baseURL:[NSBundle mainBundle].bundleURL];
Paring answered 15/8, 2021 at 9:42 Comment(0)
N
0

WebView load HTML String - making it easy - Updated & tested working - code in 2024

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()
        print("initiating webview")
        let htmlString:String! = "<body style=\"font-family: \'Poppins\', sans-serif;font-size: 48px; font-weight: 400; background-color: #000; color: #fff; margin:0; overflow-x: hidden;\"><div style=\"height: 100%;width: 100%;display: flex;justify-content: center;align-items: center;\"><div style=\"text-align: center; padding: 0 10% 0 10%;\"><h1 style=\"font-size:48px\">Welcome to sample html string load to webview in iOS.</h1><br><br><label style=\"font-size:24px\">HTML Load Webview</label><h1 style=\"font-size:32px; margin-top: 3px;\">#WKWebView</h1><br><label style=\"font-size:24px\">Youtube channel: Swiftkat Code Factory</label><h1 style=\"font-size:32px; margin-top: 3px;\">Let's Learn coding</h1><br><label style=\"font-size:24px\">Programmer</label><h1 style=\"font-size:32px; margin-top: 3px;\">Proud to be an Engineer</h1><br><label style=\"font-size:24px\">Apple Developer</label><h1 style=\"font-size:32px; margin-top: 3px;\">Enjoy Coding!!!</h1><br></div></div></body>"
        webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
    }


}

Run : Output enter image description here

Nuncle answered 29/6 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.