Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WkWebView won't loadHTMLstring

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?

like image 546
Carioni Avatar asked May 03 '16 20:05

Carioni


People also ask

How do I know if WKWebView is loaded?

To check if your WKWebView has loaded easily implement the following method: import WebKit import UIKit class ViewController: UIViewController, WKNavigationDelegate { let webView = WKWebView() func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)

Is WKWebView the same as Safari?

WKWebView - This view allows developers to embed web content in your app. You can think of WKWebView as a stripped-down version of Safari. It is responsible to load a URL request and display the web content. WKWebView has the benefit of the Nitro JavaScript engine and offers more features.

How do I import WKWebView into Objective C?

You can implement WKWebView in Objective-C, here is simple example to initiate a WKWebView : WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init]; WKWebView *webView = [[WKWebView alloc] initWithFrame:self. view. frame configuration:theConfiguration]; webView.


2 Answers

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: NSBundle.mainBundle().bundleURL)

SWIFT 3 update

webView.loadHTMLString(htmlString, baseURL: Bundle.main.bundleURL)
like image 95
A.J. Hernandez Avatar answered Sep 22 '22 07:09

A.J. Hernandez


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

like image 27
Corxit Sun Avatar answered Sep 22 '22 07:09

Corxit Sun