Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 views
in Technique[技术] by (71.8m points)

ios - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...