Renders LaTeX code in UILabel/UITextfield natively on iOS - ios

I want to render LaTeX in my application. I have tried iOSMath and iOSLatex.
The problem is iOS Math does not support latex with text while iOS Latex renders latex on a WkWebview and the takes screenshot of it and return an image.
Do we have a native renderer for LaTeX (on iOS)?
I am trying to render
Solve this equation (\left{\begin{matrix} 3x + 2y - 11 = 0 \ 2x + 3y - 9 = 0 \end{matrix}\right.)

You can use KatexMathe View
you can download this library
//MARK:- IBOutlet
#IBOutlet weak var webViewQuestion: KatexMathView!
override func viewDidLoad() {
super.viewDidLoad()
self.webViewQuestion.loadLatex("your string")
}
extension ReadQuestionVC : WKNavigationDelegate {
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webViewQuestionHC.constant = 10 //Default constant height
self.setContaintSizeWebView(webView: webViewQuestion, constantSize: self.webViewQuestionHC)
}
func setContaintSizeWebView(webView:WKWebView,constantSize:NSLayoutConstraint){
webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
webView.evaluateJavaScript("Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)", completionHandler: { (height, error) in
constantSize.constant = height as! CGFloat
webView.sizeToFit()
self.view.layoutIfNeeded()
})
}
})
}
}

Related

why I can’t open a App Store link inside a web view?

I'm getting always this error :
WebPageProxy::didFailProvisionalLoadForFrame: frameID=3, domain=WebKitErrorDomain, code=102
Normal links are working but the AppStore one is not working
what I want is the Link to open the AppStore I can't do it locally because the web is loaded from a Qualtrics web.
I try it adding the navigationAction function but that doesn't work, what I'm guessing is that maybe the request is taking some time and i need a way of load that data on an async way but to be honest i really dont know
import SwiftUI
import WebKit
struct WebView: UIViewRepresentable {
let html = """
Appstore link dont open</span></span><br />
Normal link </span></span><br />
"""
var loadStatusChanged: ((Bool, Error?) -> Void)? = nil
func makeCoordinator() -> WebView.Coordinator {
Coordinator(self)
}
func makeUIView(context: Context) -> WKWebView {
let view = WKWebView()
view.navigationDelegate = context.coordinator
view.loadHTMLString(html, baseURL: nil)
return view
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}
class Coordinator: NSObject, WKNavigationDelegate {
let parent: WebView
init(_ parent: WebView) {
self.parent = parent
}
}
}
struct ContentView: View {
var body: some View {
WebView()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Some links on tapping them might activate actions / redirect with url schemes that are non HTTPs like
_blank to open a new tab
mailto to launch the mail application
some other deep link techniques familiar to device OSs
I believe the app store link uses a combination of the above and WKWebView cannot handle non HTTPs schemes.
What you can do is to listen to URLs that fail using WKNavigationDelegate and handle them accordingly
I am not using SwiftUI but I think you can get the picture.
Set up using the same HTML as you with both the links
class ViewController: UIViewController, WKNavigationDelegate
{
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
let html = """
Appstore link dont open</span></span><br />
Normal link </span></span><br />
"""
let webview = WKWebView()
webview.frame = view.bounds
webview.navigationDelegate = self
view.addSubview(webview)
webview.loadHTMLString(html, baseURL: nil)
}
}
Then I implement these WKNavigationDelegate functions
decidePolicyFor navigationAction (documentation link) to allow even urls that do not follow the HTTPs scheme to be allowed to be processed
this navigation fail delegate function webView didFailProvisionalNavigation and check if iOS can handle the open in a new tab, mail, deep link etc so in your case it would open the app store
You could also implement the same logic as point 2 in this
WKNavigationDelegate function just in case
// MARK: WKNavigationDelegates
func webView(_ webView: WKWebView,
decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: #escaping (WKNavigationActionPolicy) -> Void)
{
decisionHandler(.allow)
}
func webView(_ webView: WKWebView,
didFailProvisionalNavigation navigation: WKNavigation!,
withError error: Error)
{
manageFailedNavigation(webView,
didFail: navigation,
withError: error)
}
func webView(_ webView: WKWebView,
didFail navigation: WKNavigation!,
withError error: Error)
{
manageFailedNavigation(webView,
didFail: navigation,
withError: error)
}
private func manageFailedNavigation(_ webView: WKWebView,
didFail navigation: WKNavigation!,
withError error: Error)
{
// Check if this failed because of mailto, _blank, deep links etc
// I have commented out how to check for a specific case like open in a new tab,
// you can try to handle each case as you wish
if error.localizedDescription
== "Redirection to URL with a scheme that is not HTTP(S)"
//let url = webView.url, url.description.lowercased().range(of: "blank") != nil
{
// Convert error to NSError so we can access the url
let nsError = error as NSError
// Get the url from the error
// This key could change in future iOS releases
if let failedURL = nsError.userInfo["NSErrorFailingURLKey"] as? URL
{
// Check if the action can be handled by iOS
if UIApplication.shared.canOpenURL(failedURL)
{
// Request iOS to open handle the link
UIApplication.shared.open(failedURL, options: [:],
completionHandler: nil)
}
}
}
}
Give this a go and check if this fixes your issue. On my side, both links seem to work fine:

iOS PDFKit textfield form input

I am currently developing a iOS app which can download PDF(Have form on it) from server to iPad, then user can fill in the form on the iPad.
The problem is we need to support chinese on the field input.
Here is the field.
When I use "Quick" input method to type any character, it repeat 3 times.
When I type one more time, it repeat again.
Do anyone have issues with typing Chinese in textfield using PDFKit on iOS?
Update, Add code work
For the server-side, just a endpoint can download pdf, the pdf already added textfield on it.
On client-side, pdf is downloaded in viewDidLoad() and set into pdfView as following code.
override func viewDidLoad() {
super.viewDidLoad()
if let code = self.code {
let url = URL(string : API.pdf + "/" + code)
let loading = self.showLoading()
var request = URLRequest(url: url!)
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request, completionHandler: { (responseData: Data?, response: URLResponse?, error: Error?) in
DispatchQueue.main.async {
self.pdfView.document = PDFDocument(data : responseData!)
self.pdfView.scaleFactor = 1
self.pdfView.backgroundColor = UIColor.lightGray
self.pdfView.autoScales = true
loading.dismiss(animated: true, completion: nil)
}
})
task.resume()
}
}
In story board, it is just a UIView and set custom class as PDFView
Updated 2
After that, i do a simple testing.
I create a simple testing controller and load pdf with just a simple textfield annotation. Result is same.
import UIKit
import PDFKit
class PDFTestViewController : ViewController {
#IBOutlet weak var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
if let url = Bundle.main.url(forResource: "4547315264964", withExtension: "pdf"),
let doc = PDFDocument(url: url){
self.pdfView.document = doc
self.pdfView.scaleFactor = 1
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}

Autoresize WKWebView to content height [duplicate]

I am experimenting with replacing a dynamically allocated instance of UIWebView with a WKWebView instance when running under iOS 8 and newer, and I cannot find a way to determine the content size of a WKWebView.
My web view is embedded within a larger UIScrollView container, and therefore I need to determine the ideal size for the web view. This will allow me to modify its frame to show all of its HTML content without the need to scroll within the web view, and I will be able to set the correct height for the scroll view container (by setting scrollview.contentSize).
I have tried sizeToFit and sizeThatFits without success. Here is my code that creates a WKWebView instance and adds it to the container scrollview:
// self.view is a UIScrollView sized to something like 320.0 x 400.0.
CGRect wvFrame = CGRectMake(0, 0, self.view.frame.size.width, 100.0);
self.mWebView = [[[WKWebView alloc] initWithFrame:wvFrame] autorelease];
self.mWebView.navigationDelegate = self;
self.mWebView.scrollView.bounces = NO;
self.mWebView.scrollView.scrollEnabled = NO;
NSString *s = ... // Load s from a Core Data field.
[self.mWebView loadHTMLString:s baseURL:nil];
[self.view addSubview:self.mWebView];
Here is an experimental didFinishNavigation method:
- (void)webView:(WKWebView *)aWebView
didFinishNavigation:(WKNavigation *)aNavigation
{
CGRect wvFrame = aWebView.frame;
NSLog(#"original wvFrame: %#\n", NSStringFromCGRect(wvFrame));
[aWebView sizeToFit];
NSLog(#"wvFrame after sizeToFit: %#\n", NSStringFromCGRect(wvFrame));
wvFrame.size.height = 1.0;
aWebView.frame = wvFrame;
CGSize sz = [aWebView sizeThatFits:CGSizeZero];
NSLog(#"sizeThatFits A: %#\n", NSStringFromCGSize(sz));
sz = CGSizeMake(wvFrame.size.width, 0.0);
sz = [aWebView sizeThatFits:sz];
NSLog(#"sizeThatFits B: %#\n", NSStringFromCGSize(sz));
}
And here is the output that is generated:
2014-12-16 17:29:38.055 App[...] original wvFrame: {{0, 0}, {320, 100}}
2014-12-16 17:29:38.055 App[...] wvFrame after sizeToFit: {{0, 0}, {320, 100}}
2014-12-16 17:29:38.056 App[...] wvFrame after sizeThatFits A: {320, 1}
2014-12-16 17:29:38.056 App[...] wvFrame after sizeThatFits B: {320, 1}
The sizeToFit call has no effect and sizeThatFits always returns a height of 1.
I think I read every answer on this subject and all I had was part of the solution. Most of the time I spent trying to implement KVO method as described by #davew, which occasionally worked, but most of the time left a white space under the content of a WKWebView container. I also implemented #David Beck suggestion and made the container height to be 0 thus avoiding the possibility that the problem occurs if the container height is larger that that of the content. In spite of that I had that occasional blank space.
So, for me, "contentSize" observer had a lot of flaws. I do not have a lot of experience with web technologies so I cannot answer what was the problem with this solution, but i saw that if I only print height in the console but do not do anything with it (eg. resize the constraints), it jumps to some number (e.g. 5000) and than goes to the number before that highest one (e.g. 2500 - which turns out to be the correct one). If I do set the height constraint to the height which I get from "contentSize" it sets itself to the highest number it gets and never gets resized to the correct one - which is, again, mentioned by #David Beck comment.
After lots of experiments I've managed to find a solution that works for me:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
self.containerHeight.constant = height as! CGFloat
})
}
})
}
Of course, it is important to set the constraints correctly so that scrollView resizes according to the containerHeight constraint.
As it turns out didFinish navigation method never gets called when I wanted, but having set document.readyState step, the next one (document.body.offsetHeight) gets called at the right moment, returning me the right number for height.
You could use Key-Value Observing (KVO)...
In your ViewController:
- (void)viewDidLoad {
...
[self.webView.scrollView addObserver:self forKeyPath:#"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)dealloc
{
[self.webView.scrollView removeObserver:self forKeyPath:#"contentSize" context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (object == self.webView.scrollView && [keyPath isEqual:#"contentSize"]) {
// we are here because the contentSize of the WebView's scrollview changed.
UIScrollView *scrollView = self.webView.scrollView;
NSLog(#"New contentSize: %f x %f", scrollView.contentSize.width, scrollView.contentSize.height);
}
}
This would save the use of JavaScript and keep you in the loop on all changes.
I had to deal with this issue myself recently. In the end, I was using a modification of the solution proposed by Chris McClenaghan.
Actually, his original solution is pretty good and it works in most simple cases. However, it only worked for me on pages with text. It probably also works on pages with images that have a static height. However, it definitely doesn't work when you have images whose size is defined with max-height and max-width attributes.
And this is because those elements can get resized after the page is loaded. So, actually, the height returned in onLoad will always be correct. But it will only be correct for that particular instance. The workaround is to monitor the change of the body height and respond to it.
Monitor resizing of the document.body
var shouldListenToResizeNotification = false
lazy var webView:WKWebView = {
//Javascript string
let source = "window.onload=function () {window.webkit.messageHandlers.sizeNotification.postMessage({justLoaded:true,height: document.body.scrollHeight});};"
let source2 = "document.body.addEventListener( 'resize', incrementCounter); function incrementCounter() {window.webkit.messageHandlers.sizeNotification.postMessage({height: document.body.scrollHeight});};"
//UserScript object
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let script2 = WKUserScript(source: source2, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
//Content Controller object
let controller = WKUserContentController()
//Add script to controller
controller.addUserScript(script)
controller.addUserScript(script2)
//Add message handler reference
controller.add(self, name: "sizeNotification")
//Create configuration
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
return WKWebView(frame: CGRect.zero, configuration: configuration)
}()
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let responseDict = message.body as? [String:Any],
let height = responseDict["height"] as? Float else {return}
if self.webViewHeightConstraint.constant != CGFloat(height) {
if let _ = responseDict["justLoaded"] {
print("just loaded")
shouldListenToResizeNotification = true
self.webViewHeightConstraint.constant = CGFloat(height)
}
else if shouldListenToResizeNotification {
print("height is \(height)")
self.webViewHeightConstraint.constant = CGFloat(height)
}
}
}
This solution is by far the most elegant that I could come up with. There are, however, two things you should be aware of.
Firstly, before loading your URL you should set shouldListenToResizeNotification to false. This extra logic is needed for cases when the loaded URL can change rapidly. When this occurs, notifications from old content for some reason can overlap with those from the new content. To prevent such behaviour, I created this variable. It ensures that once we start loading new content we no longer process notification from the old one and we only resume processing of resize notifications after new content is loaded.
Most importantly, however, you need to be aware about this:
If you adopt this solution you need to take into account that if you change the size of your WKWebView to anything other than the size reported by the notification - the notification will be triggered again.
Be careful with this as it is easy to enter an infinite loop. For example, if you decide to handle the notification by making your height equal to reported height + some extra padding:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let responseDict = message.body as? [String:Float],
let height = responseDict["height"] else {return}
self.webViewHeightConstraint.constant = CGFloat(height+8)
}
As you can see, because I am adding 8 to the reported height, after this is done the size of my body will change and the notification will be posted again.
Be alert to such situations and otherwise you should be fine.
And please let me know if you discover any problems with this solution - I am relying on it myself so it is best to know if there are some faults which I haven't spotted!
Works for me
extension TransactionDetailViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.webviewHeightConstraint.constant = webView.scrollView.contentSize.height
}
}
}
Try the following. Wherever you instantiate your WKWebView instance, add something similar to the following:
//Javascript string
NSString * source = #"window.webkit.messageHandlers.sizeNotification.postMessage({width: document.width, height: document.height});";
//UserScript object
WKUserScript * script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//Content Controller object
WKUserContentController * controller = [[WKUserContentController alloc] init];
//Add script to controller
[controller addUserScript:script];
//Add message handler reference
[controller addScriptMessageHandler:self name:#"sizeNotification"];
//Create configuration
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
//Add controller to configuration
configuration.userContentController = controller;
//Use whatever you require for WKWebView frame
CGRect frame = CGRectMake(...?);
//Create your WKWebView instance with the configuration
WKWebView * webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
//Assign delegate if necessary
webView.navigationDelegate = self;
//Load html
[webView loadHTMLString:#"some html ..." baseURL:[[NSBundle mainBundle] bundleURL]];
Then add a method similar to the following to which ever class obeys WKScriptMessageHandler protocol to handle the message:
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
CGRect frame = message.webView.frame;
frame.size.height = [[message.body valueForKey:#"height"] floatValue];
message.webView.frame = frame;}
This works for me.
If you have more than text in your document you may need to wrap the javascript like this to ensure everything is loaded:
#"window.onload=function () { window.webkit.messageHandlers.sizeNotification.postMessage({width: document.width, height: document.height});};"
NOTE: This solution does not address ongoing updates to the document.
Most answers are using "document.body.offsetHeight".
This hides the last object of the body.
I overcame this issue by using a KVO observer listening for changes in WKWebview "contentSize", then running this code:
self.webView.evaluateJavaScript(
"(function() {var i = 1, result = 0; while(true){result =
document.body.children[document.body.children.length - i].offsetTop +
document.body.children[document.body.children.length - i].offsetHeight;
if (result > 0) return result; i++}})()",
completionHandler: { (height, error) in
let height = height as! CGFloat
self.webViewHeightConstraint.constant = height
}
)
It's not the prettiest code possible, but it worked for me.
You can also got content height of WKWebView by evaluateJavaScript.
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[webView evaluateJavaScript:#"Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)"
completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (!error) {
CGFloat height = [result floatValue];
// do with the height
}
}];
}
You need to wait for the webview to finish loading. Here is a working example I used
WKWebView Content loaded function never get called
Then after webview has finished loading, then you can determine the heights you need by
func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) {
println(webView.scrollView.contentSize.height)
}
I found that the answer by hlung here, extending the WKWebView as follows was the simplest and most effective solution for me:
https://gist.github.com/pkuecuekyan/f70096218a6b969e0249427a7d324f91
His comment follows:
"Nice! For me, instead of setting the webView.frame, I set autolayout intrinsicContentSize."
And his code was as follows:
import UIKit
import WebKit
class ArticleWebView: WKWebView {
init(frame: CGRect) {
let configuration = WKWebViewConfiguration()
super.init(frame: frame, configuration: configuration)
self.navigationDelegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
return self.scrollView.contentSize
}
}
extension ArticleWebView: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.readyState", completionHandler: { (_, _) in
webView.invalidateIntrinsicContentSize()
})
}
}
This is a slight edit of #IvanMih's answer. For those of you experiencing a large white space at the end of your WKWebview this solution worked well for me:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
let height = webView.scrollView.contentSize
print("height of webView is: \(height)")
}
})
}
so basically instead of calculating the height based on scrollHeight you calculate height using webView.scrollView.contentSize. I'm sure there are scenarios where this will break, but I think it'll do pretty well for static content and if you are displaying all the content without the user having to scroll.
After lots of experiments I've managed to find a solution that works for me I found to make a webview heigh dynamic without using evaluating javascript and also without taking height constant from webview this work with me like a charm and also work when I inject new style to HTML and play with font sizes and heights
code in Swift
1- give your Webview navigation delegate
webView.navigationDelegate = self
2- in delegation extension
extension yourclass : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Handel Dynamic Height For Webview Loads with HTML
// Most important to reset webview height to any desired height i prefer 1 or 0
webView.frame.size.height = 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// here get height constant and assign new height in it
if let constraint = (webView.constraints.filter{$0.firstAttribute == .height}.first) {
constraint.constant = webView.scrollView.contentSize.height
}
}
hope it works also with you guys
** note this not my entire effort I searched a lot in StackOverflow and other sites and this is what finally works with me with a lot of testing also
using #Andriy's answer and this answer i was able to set get height of contentSize in WKWebView and change it's height.
here is full swift 4 code:
var neededConstraints: [NSLayoutConstraint] = []
#IBOutlet weak var webViewContainer: UIView!
#IBOutlet weak var webViewHeight: NSLayoutConstraint! {
didSet {
if oldValue != nil, oldValue.constant != webViewHeight.constant {
view.layoutIfNeeded()
}
}
}
lazy var webView: WKWebView = {
var source = """
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
};
})();
// Observe a specific DOM element:
observeDOM( document.body ,function(){
window.webkit.messageHandlers.sizeNotification.postMessage({'scrollHeight': document.body.scrollHeight,'offsetHeight':document.body.offsetHeight,'clientHeight':document.body.clientHeight});
});
"""
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let controller = WKUserContentController()
controller.addUserScript(script)
controller.add(self, name: "sizeNotification")
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
let this = WKWebView(frame: .zero, configuration: configuration)
webViewContainer.addSubview(this)
this.translatesAutoresizingMaskIntoConstraints = false
this.scrollView.isScrollEnabled = false
// constraint for webview when added to it's superview
neededConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[web]|",
options: [],
metrics: nil,
views: ["web": this])
neededConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[web]|",
options: [],
metrics: nil,
views: ["web": this])
return this
}()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
_ = webView // to create constraints needed for webView
NSLayoutConstraint.activate(neededConstraints)
let url = URL(string: "https://www.awwwards.com/")!
let request = URLRequest(url: url)
webView.load(request)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let body = message.body as? Dictionary<String, CGFloat>,
let scrollHeight = body["scrollHeight"],
let offsetHeight = body["offsetHeight"],
let clientHeight = body["clientHeight"] {
webViewHeight.constant = scrollHeight
print(scrollHeight, offsetHeight, clientHeight)
}
}
I've tried the scroll view KVO and I've tried evaluating javascript on the document, using clientHeight, offsetHeight, etc...
What worked for me eventually is: document.body.scrollHeight. Or use the scrollHeight of your top most element, e.g. a container div.
I listen to the loading WKWebview property changes using KVO:
[webview addObserver: self forKeyPath: NSStringFromSelector(#selector(loading)) options: NSKeyValueObservingOptionNew context: nil];
And then:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if(object == self.webview && [keyPath isEqualToString: NSStringFromSelector(#selector(loading))]) {
NSNumber *newValue = change[NSKeyValueChangeNewKey];
if(![newValue boolValue]) {
[self updateWebviewFrame];
}
}
}
The updateWebviewFrame implementation:
[self.webview evaluateJavaScript: #"document.body.scrollHeight" completionHandler: ^(id response, NSError *error) {
CGRect frame = self.webview.frame;
frame.size.height = [response floatValue];
self.webview.frame = frame;
}];
I tried Javascript version in UITableViewCell, and it works perfectly. However, if you want to put it in the scrollView. I don't know why, the height can be higher but cannot be shorter. However, I found a UIWebView solution here. https://stackoverflow.com/a/48887971/5514452
It also works in WKWebView. I think the problem is because the WebView need relayout, but somehow it will not shrink and can only enlarge. We need to reset the height and it will definitely resize.
Edit: I reset the frame height after setting the constraint because sometime it will not working due to setting the frame height to 0.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView.frame.size.height = 0
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
let webViewHeight = height as! CGFloat
self.webViewHeightConstraint.constant = webViewHeight
self.webView.frame.size.height = webViewHeight
})
}
})
}
Also tried to implement different methods and finally came to a solution. As a result I made a self-sizing WKWebView, that adapts its intrinsicContentSize to the size of its contents. So you can use it in Auto Layouts. As an example I made a view, which might help you display math formula on iOS apps: https://github.com/Mazorati/SVLatexView
The following code has worked perfectly for me, for any content in the webkit. Make sure to add the following delegate to your class: WKNavigationDelegate.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.bodyWebView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.bodyWebView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
let heightWebView = height as! CGFloat
//heightWebView is the height of the web view
})
}
})
}
}
The dispatch is important, because this way you ensure that the height obtained at the end of loading the web view is correct, this happens because of the type of elements that the html may have.
I want to contribute with the solution for a special case that is not mentioned in the answers above and that may happen to you if you are using custom fonts in your WKWebView.
I tried ALL the solutions explained here, and many others mentioned in other StackOverflow questions. Nothing was working 100% correctly for me. I had always the same problem: the height returned was always a little smaller than the real height of the WkWebView. I tried WKNavigationDelegate way, and I tried to listen to self-generated events by injecting js into the HTML rendered, without success, the height was always wrong in all cases.
The first thing I learned: the webview has to be added to the layout before loading the html and waiting for the finished event. If you try to render the webview in an isolated way without adding it before to the layout, then the height will be very wrong.
Curiously, I found out that setting a breakpoint after the html was rendered, and before calling the height evaluation method, then the returned height was correct. It was not important which height was measured (scrollHeight or offsetheight), both were always correct.
That pointed me in the right direction. The conclusion was obvious (although I needed a lot of days making debug to realize it): after the didFinishNavigation event is received, or if you are using custom js and listening to the window.onload event or similar, the height returned is almost correct but not completely because the rendering is not finished yet.
As explained here, Firefox, Chrome, and Safari trigger the DomContenLoaded event before the font-face is applied to the document (and maybe, before the css is applied to the document too?). In my case, I was using a custom font embedded in my app and referenced in the HTML in the classical way:
<style>
#font-face {
font-family: 'CustomFont';
src: url('montserrat.ttf');
format('truetype');
}
body{
font-family: 'CustomFont';
font-size: 12px;
}
Solution? You have to listen to the event document.fonts.ready, that happens after the event window.onload and the like. Embeed the following js in the html you are loading in the WkWebView:
document.fonts.ready.then(function() {
window.webkit.messageHandlers.iosEventListener.postMessage('custom_event_fonts_ready');
});
Then in your iOS app, listen to the event with
self.webView.configuration.userContentController.add(self, name: "iosEventListener")
and when received
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let body = message.body as? String {
if (body == "custom_event_fonts_ready") {
self.evaluateBodyHeight()
}
}
}
private func evaluateBodyHeight() {
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
let webViewHeight = height as! CGFloat
//Do something with the height.
})
}
})
}
I'm not sure, but I think that with this solution, all the different ways to measure the height of web view will return the correct one. After almost one month of debugging and being desperate, I have no desire to test them
Apologizes for my bad English.
The best way is to observe contentSize property of webView.scrollView and update height constraint of webView accordingly:
private var contentSizeObserver: NSKeyValueObservation?
contentSizeObserver = webView.scrollView.observe(\.contentSize, options: .new) { [weak self] _, change in
guard let contentSize = change.newValue else { return }
self?.csWebViewHeight?.update(offset: contentSize.height)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Recalculate webView size
csWebViewHeight?.update(offset: 0)
webView.setNeedsLayout()
webView.layoutIfNeeded()
}
None of the listed JS funcs to get the content height worked reliably for me. What I found to consistently work is to find the last element in the DOM and get its position explicitly:
webView.evaluateJavaScript(
"document.body.lastChild.getBoundingClientRect().bottom + window.scrollY"
) { [weak self] (result, _) in
guard let self = self,
let height = result as? CGFloat,
height > 0 else { return }
self.heightConstraint?.constant = height
}

UIWebView FinishLoad/Detection issue

when using a UIWebView I want to detect the moment a web page finishes loading in an iOS app.
For that I tried to use the solution mentioned in this post.
But the problem is that the webViewDidFinishLoad() function gets called (almost right after the call to loadRequest()) way before the page appears on the display making the solution useless.
Am I missing something? Or is there a better (or different) way?
P.S. I am using Xcode Version 8.3.2 and iOS Version 10.3.2
The every page of HTML content loaded webViewDidFinishLoad will called... So I put three types of validation. Use it which one is preferred for you.
var webViewPages = 0
func webViewDidStartLoad(_ webView: UIWebView) {
webViewPages += 1
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
webViewPages -= 1
}
func webViewDidFinishLoad(_ webView: UIWebView) {
if (webView.isLoading){
return
}
if webView.stringByEvaluatingJavaScript(from: "document.readyState") != "complete"{
return
}
webViewPages -= 1
if webViewPages > 0{
return
}
// Calculating webview height
let height = Int(webView.stringByEvaluatingJavaScript(from: "document.body.scrollHeight")!)
}

How to determine the content size of a WKWebView?

I am experimenting with replacing a dynamically allocated instance of UIWebView with a WKWebView instance when running under iOS 8 and newer, and I cannot find a way to determine the content size of a WKWebView.
My web view is embedded within a larger UIScrollView container, and therefore I need to determine the ideal size for the web view. This will allow me to modify its frame to show all of its HTML content without the need to scroll within the web view, and I will be able to set the correct height for the scroll view container (by setting scrollview.contentSize).
I have tried sizeToFit and sizeThatFits without success. Here is my code that creates a WKWebView instance and adds it to the container scrollview:
// self.view is a UIScrollView sized to something like 320.0 x 400.0.
CGRect wvFrame = CGRectMake(0, 0, self.view.frame.size.width, 100.0);
self.mWebView = [[[WKWebView alloc] initWithFrame:wvFrame] autorelease];
self.mWebView.navigationDelegate = self;
self.mWebView.scrollView.bounces = NO;
self.mWebView.scrollView.scrollEnabled = NO;
NSString *s = ... // Load s from a Core Data field.
[self.mWebView loadHTMLString:s baseURL:nil];
[self.view addSubview:self.mWebView];
Here is an experimental didFinishNavigation method:
- (void)webView:(WKWebView *)aWebView
didFinishNavigation:(WKNavigation *)aNavigation
{
CGRect wvFrame = aWebView.frame;
NSLog(#"original wvFrame: %#\n", NSStringFromCGRect(wvFrame));
[aWebView sizeToFit];
NSLog(#"wvFrame after sizeToFit: %#\n", NSStringFromCGRect(wvFrame));
wvFrame.size.height = 1.0;
aWebView.frame = wvFrame;
CGSize sz = [aWebView sizeThatFits:CGSizeZero];
NSLog(#"sizeThatFits A: %#\n", NSStringFromCGSize(sz));
sz = CGSizeMake(wvFrame.size.width, 0.0);
sz = [aWebView sizeThatFits:sz];
NSLog(#"sizeThatFits B: %#\n", NSStringFromCGSize(sz));
}
And here is the output that is generated:
2014-12-16 17:29:38.055 App[...] original wvFrame: {{0, 0}, {320, 100}}
2014-12-16 17:29:38.055 App[...] wvFrame after sizeToFit: {{0, 0}, {320, 100}}
2014-12-16 17:29:38.056 App[...] wvFrame after sizeThatFits A: {320, 1}
2014-12-16 17:29:38.056 App[...] wvFrame after sizeThatFits B: {320, 1}
The sizeToFit call has no effect and sizeThatFits always returns a height of 1.
I think I read every answer on this subject and all I had was part of the solution. Most of the time I spent trying to implement KVO method as described by #davew, which occasionally worked, but most of the time left a white space under the content of a WKWebView container. I also implemented #David Beck suggestion and made the container height to be 0 thus avoiding the possibility that the problem occurs if the container height is larger that that of the content. In spite of that I had that occasional blank space.
So, for me, "contentSize" observer had a lot of flaws. I do not have a lot of experience with web technologies so I cannot answer what was the problem with this solution, but i saw that if I only print height in the console but do not do anything with it (eg. resize the constraints), it jumps to some number (e.g. 5000) and than goes to the number before that highest one (e.g. 2500 - which turns out to be the correct one). If I do set the height constraint to the height which I get from "contentSize" it sets itself to the highest number it gets and never gets resized to the correct one - which is, again, mentioned by #David Beck comment.
After lots of experiments I've managed to find a solution that works for me:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
self.containerHeight.constant = height as! CGFloat
})
}
})
}
Of course, it is important to set the constraints correctly so that scrollView resizes according to the containerHeight constraint.
As it turns out didFinish navigation method never gets called when I wanted, but having set document.readyState step, the next one (document.body.offsetHeight) gets called at the right moment, returning me the right number for height.
You could use Key-Value Observing (KVO)...
In your ViewController:
- (void)viewDidLoad {
...
[self.webView.scrollView addObserver:self forKeyPath:#"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
- (void)dealloc
{
[self.webView.scrollView removeObserver:self forKeyPath:#"contentSize" context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (object == self.webView.scrollView && [keyPath isEqual:#"contentSize"]) {
// we are here because the contentSize of the WebView's scrollview changed.
UIScrollView *scrollView = self.webView.scrollView;
NSLog(#"New contentSize: %f x %f", scrollView.contentSize.width, scrollView.contentSize.height);
}
}
This would save the use of JavaScript and keep you in the loop on all changes.
I had to deal with this issue myself recently. In the end, I was using a modification of the solution proposed by Chris McClenaghan.
Actually, his original solution is pretty good and it works in most simple cases. However, it only worked for me on pages with text. It probably also works on pages with images that have a static height. However, it definitely doesn't work when you have images whose size is defined with max-height and max-width attributes.
And this is because those elements can get resized after the page is loaded. So, actually, the height returned in onLoad will always be correct. But it will only be correct for that particular instance. The workaround is to monitor the change of the body height and respond to it.
Monitor resizing of the document.body
var shouldListenToResizeNotification = false
lazy var webView:WKWebView = {
//Javascript string
let source = "window.onload=function () {window.webkit.messageHandlers.sizeNotification.postMessage({justLoaded:true,height: document.body.scrollHeight});};"
let source2 = "document.body.addEventListener( 'resize', incrementCounter); function incrementCounter() {window.webkit.messageHandlers.sizeNotification.postMessage({height: document.body.scrollHeight});};"
//UserScript object
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let script2 = WKUserScript(source: source2, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
//Content Controller object
let controller = WKUserContentController()
//Add script to controller
controller.addUserScript(script)
controller.addUserScript(script2)
//Add message handler reference
controller.add(self, name: "sizeNotification")
//Create configuration
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
return WKWebView(frame: CGRect.zero, configuration: configuration)
}()
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let responseDict = message.body as? [String:Any],
let height = responseDict["height"] as? Float else {return}
if self.webViewHeightConstraint.constant != CGFloat(height) {
if let _ = responseDict["justLoaded"] {
print("just loaded")
shouldListenToResizeNotification = true
self.webViewHeightConstraint.constant = CGFloat(height)
}
else if shouldListenToResizeNotification {
print("height is \(height)")
self.webViewHeightConstraint.constant = CGFloat(height)
}
}
}
This solution is by far the most elegant that I could come up with. There are, however, two things you should be aware of.
Firstly, before loading your URL you should set shouldListenToResizeNotification to false. This extra logic is needed for cases when the loaded URL can change rapidly. When this occurs, notifications from old content for some reason can overlap with those from the new content. To prevent such behaviour, I created this variable. It ensures that once we start loading new content we no longer process notification from the old one and we only resume processing of resize notifications after new content is loaded.
Most importantly, however, you need to be aware about this:
If you adopt this solution you need to take into account that if you change the size of your WKWebView to anything other than the size reported by the notification - the notification will be triggered again.
Be careful with this as it is easy to enter an infinite loop. For example, if you decide to handle the notification by making your height equal to reported height + some extra padding:
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let responseDict = message.body as? [String:Float],
let height = responseDict["height"] else {return}
self.webViewHeightConstraint.constant = CGFloat(height+8)
}
As you can see, because I am adding 8 to the reported height, after this is done the size of my body will change and the notification will be posted again.
Be alert to such situations and otherwise you should be fine.
And please let me know if you discover any problems with this solution - I am relying on it myself so it is best to know if there are some faults which I haven't spotted!
Works for me
extension TransactionDetailViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.webviewHeightConstraint.constant = webView.scrollView.contentSize.height
}
}
}
Try the following. Wherever you instantiate your WKWebView instance, add something similar to the following:
//Javascript string
NSString * source = #"window.webkit.messageHandlers.sizeNotification.postMessage({width: document.width, height: document.height});";
//UserScript object
WKUserScript * script = [[WKUserScript alloc] initWithSource:source injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//Content Controller object
WKUserContentController * controller = [[WKUserContentController alloc] init];
//Add script to controller
[controller addUserScript:script];
//Add message handler reference
[controller addScriptMessageHandler:self name:#"sizeNotification"];
//Create configuration
WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
//Add controller to configuration
configuration.userContentController = controller;
//Use whatever you require for WKWebView frame
CGRect frame = CGRectMake(...?);
//Create your WKWebView instance with the configuration
WKWebView * webView = [[WKWebView alloc] initWithFrame:frame configuration:configuration];
//Assign delegate if necessary
webView.navigationDelegate = self;
//Load html
[webView loadHTMLString:#"some html ..." baseURL:[[NSBundle mainBundle] bundleURL]];
Then add a method similar to the following to which ever class obeys WKScriptMessageHandler protocol to handle the message:
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
CGRect frame = message.webView.frame;
frame.size.height = [[message.body valueForKey:#"height"] floatValue];
message.webView.frame = frame;}
This works for me.
If you have more than text in your document you may need to wrap the javascript like this to ensure everything is loaded:
#"window.onload=function () { window.webkit.messageHandlers.sizeNotification.postMessage({width: document.width, height: document.height});};"
NOTE: This solution does not address ongoing updates to the document.
Most answers are using "document.body.offsetHeight".
This hides the last object of the body.
I overcame this issue by using a KVO observer listening for changes in WKWebview "contentSize", then running this code:
self.webView.evaluateJavaScript(
"(function() {var i = 1, result = 0; while(true){result =
document.body.children[document.body.children.length - i].offsetTop +
document.body.children[document.body.children.length - i].offsetHeight;
if (result > 0) return result; i++}})()",
completionHandler: { (height, error) in
let height = height as! CGFloat
self.webViewHeightConstraint.constant = height
}
)
It's not the prettiest code possible, but it worked for me.
You can also got content height of WKWebView by evaluateJavaScript.
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[webView evaluateJavaScript:#"Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)"
completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (!error) {
CGFloat height = [result floatValue];
// do with the height
}
}];
}
You need to wait for the webview to finish loading. Here is a working example I used
WKWebView Content loaded function never get called
Then after webview has finished loading, then you can determine the heights you need by
func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!) {
println(webView.scrollView.contentSize.height)
}
I found that the answer by hlung here, extending the WKWebView as follows was the simplest and most effective solution for me:
https://gist.github.com/pkuecuekyan/f70096218a6b969e0249427a7d324f91
His comment follows:
"Nice! For me, instead of setting the webView.frame, I set autolayout intrinsicContentSize."
And his code was as follows:
import UIKit
import WebKit
class ArticleWebView: WKWebView {
init(frame: CGRect) {
let configuration = WKWebViewConfiguration()
super.init(frame: frame, configuration: configuration)
self.navigationDelegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override var intrinsicContentSize: CGSize {
return self.scrollView.contentSize
}
}
extension ArticleWebView: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.readyState", completionHandler: { (_, _) in
webView.invalidateIntrinsicContentSize()
})
}
}
This is a slight edit of #IvanMih's answer. For those of you experiencing a large white space at the end of your WKWebview this solution worked well for me:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
let height = webView.scrollView.contentSize
print("height of webView is: \(height)")
}
})
}
so basically instead of calculating the height based on scrollHeight you calculate height using webView.scrollView.contentSize. I'm sure there are scenarios where this will break, but I think it'll do pretty well for static content and if you are displaying all the content without the user having to scroll.
After lots of experiments I've managed to find a solution that works for me I found to make a webview heigh dynamic without using evaluating javascript and also without taking height constant from webview this work with me like a charm and also work when I inject new style to HTML and play with font sizes and heights
code in Swift
1- give your Webview navigation delegate
webView.navigationDelegate = self
2- in delegation extension
extension yourclass : WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Handel Dynamic Height For Webview Loads with HTML
// Most important to reset webview height to any desired height i prefer 1 or 0
webView.frame.size.height = 1
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// here get height constant and assign new height in it
if let constraint = (webView.constraints.filter{$0.firstAttribute == .height}.first) {
constraint.constant = webView.scrollView.contentSize.height
}
}
hope it works also with you guys
** note this not my entire effort I searched a lot in StackOverflow and other sites and this is what finally works with me with a lot of testing also
using #Andriy's answer and this answer i was able to set get height of contentSize in WKWebView and change it's height.
here is full swift 4 code:
var neededConstraints: [NSLayoutConstraint] = []
#IBOutlet weak var webViewContainer: UIView!
#IBOutlet weak var webViewHeight: NSLayoutConstraint! {
didSet {
if oldValue != nil, oldValue.constant != webViewHeight.constant {
view.layoutIfNeeded()
}
}
}
lazy var webView: WKWebView = {
var source = """
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
};
})();
// Observe a specific DOM element:
observeDOM( document.body ,function(){
window.webkit.messageHandlers.sizeNotification.postMessage({'scrollHeight': document.body.scrollHeight,'offsetHeight':document.body.offsetHeight,'clientHeight':document.body.clientHeight});
});
"""
let script = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let controller = WKUserContentController()
controller.addUserScript(script)
controller.add(self, name: "sizeNotification")
let configuration = WKWebViewConfiguration()
configuration.userContentController = controller
let this = WKWebView(frame: .zero, configuration: configuration)
webViewContainer.addSubview(this)
this.translatesAutoresizingMaskIntoConstraints = false
this.scrollView.isScrollEnabled = false
// constraint for webview when added to it's superview
neededConstraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[web]|",
options: [],
metrics: nil,
views: ["web": this])
neededConstraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[web]|",
options: [],
metrics: nil,
views: ["web": this])
return this
}()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
_ = webView // to create constraints needed for webView
NSLayoutConstraint.activate(neededConstraints)
let url = URL(string: "https://www.awwwards.com/")!
let request = URLRequest(url: url)
webView.load(request)
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let body = message.body as? Dictionary<String, CGFloat>,
let scrollHeight = body["scrollHeight"],
let offsetHeight = body["offsetHeight"],
let clientHeight = body["clientHeight"] {
webViewHeight.constant = scrollHeight
print(scrollHeight, offsetHeight, clientHeight)
}
}
I've tried the scroll view KVO and I've tried evaluating javascript on the document, using clientHeight, offsetHeight, etc...
What worked for me eventually is: document.body.scrollHeight. Or use the scrollHeight of your top most element, e.g. a container div.
I listen to the loading WKWebview property changes using KVO:
[webview addObserver: self forKeyPath: NSStringFromSelector(#selector(loading)) options: NSKeyValueObservingOptionNew context: nil];
And then:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if(object == self.webview && [keyPath isEqualToString: NSStringFromSelector(#selector(loading))]) {
NSNumber *newValue = change[NSKeyValueChangeNewKey];
if(![newValue boolValue]) {
[self updateWebviewFrame];
}
}
}
The updateWebviewFrame implementation:
[self.webview evaluateJavaScript: #"document.body.scrollHeight" completionHandler: ^(id response, NSError *error) {
CGRect frame = self.webview.frame;
frame.size.height = [response floatValue];
self.webview.frame = frame;
}];
I tried Javascript version in UITableViewCell, and it works perfectly. However, if you want to put it in the scrollView. I don't know why, the height can be higher but cannot be shorter. However, I found a UIWebView solution here. https://stackoverflow.com/a/48887971/5514452
It also works in WKWebView. I think the problem is because the WebView need relayout, but somehow it will not shrink and can only enlarge. We need to reset the height and it will definitely resize.
Edit: I reset the frame height after setting the constraint because sometime it will not working due to setting the frame height to 0.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.webView.frame.size.height = 0
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
let webViewHeight = height as! CGFloat
self.webViewHeightConstraint.constant = webViewHeight
self.webView.frame.size.height = webViewHeight
})
}
})
}
Also tried to implement different methods and finally came to a solution. As a result I made a self-sizing WKWebView, that adapts its intrinsicContentSize to the size of its contents. So you can use it in Auto Layouts. As an example I made a view, which might help you display math formula on iOS apps: https://github.com/Mazorati/SVLatexView
The following code has worked perfectly for me, for any content in the webkit. Make sure to add the following delegate to your class: WKNavigationDelegate.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.bodyWebView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.bodyWebView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
let heightWebView = height as! CGFloat
//heightWebView is the height of the web view
})
}
})
}
}
The dispatch is important, because this way you ensure that the height obtained at the end of loading the web view is correct, this happens because of the type of elements that the html may have.
I want to contribute with the solution for a special case that is not mentioned in the answers above and that may happen to you if you are using custom fonts in your WKWebView.
I tried ALL the solutions explained here, and many others mentioned in other StackOverflow questions. Nothing was working 100% correctly for me. I had always the same problem: the height returned was always a little smaller than the real height of the WkWebView. I tried WKNavigationDelegate way, and I tried to listen to self-generated events by injecting js into the HTML rendered, without success, the height was always wrong in all cases.
The first thing I learned: the webview has to be added to the layout before loading the html and waiting for the finished event. If you try to render the webview in an isolated way without adding it before to the layout, then the height will be very wrong.
Curiously, I found out that setting a breakpoint after the html was rendered, and before calling the height evaluation method, then the returned height was correct. It was not important which height was measured (scrollHeight or offsetheight), both were always correct.
That pointed me in the right direction. The conclusion was obvious (although I needed a lot of days making debug to realize it): after the didFinishNavigation event is received, or if you are using custom js and listening to the window.onload event or similar, the height returned is almost correct but not completely because the rendering is not finished yet.
As explained here, Firefox, Chrome, and Safari trigger the DomContenLoaded event before the font-face is applied to the document (and maybe, before the css is applied to the document too?). In my case, I was using a custom font embedded in my app and referenced in the HTML in the classical way:
<style>
#font-face {
font-family: 'CustomFont';
src: url('montserrat.ttf');
format('truetype');
}
body{
font-family: 'CustomFont';
font-size: 12px;
}
Solution? You have to listen to the event document.fonts.ready, that happens after the event window.onload and the like. Embeed the following js in the html you are loading in the WkWebView:
document.fonts.ready.then(function() {
window.webkit.messageHandlers.iosEventListener.postMessage('custom_event_fonts_ready');
});
Then in your iOS app, listen to the event with
self.webView.configuration.userContentController.add(self, name: "iosEventListener")
and when received
public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let body = message.body as? String {
if (body == "custom_event_fonts_ready") {
self.evaluateBodyHeight()
}
}
}
private func evaluateBodyHeight() {
self.webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
self.webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
let webViewHeight = height as! CGFloat
//Do something with the height.
})
}
})
}
I'm not sure, but I think that with this solution, all the different ways to measure the height of web view will return the correct one. After almost one month of debugging and being desperate, I have no desire to test them
Apologizes for my bad English.
The best way is to observe contentSize property of webView.scrollView and update height constraint of webView accordingly:
private var contentSizeObserver: NSKeyValueObservation?
contentSizeObserver = webView.scrollView.observe(\.contentSize, options: .new) { [weak self] _, change in
guard let contentSize = change.newValue else { return }
self?.csWebViewHeight?.update(offset: contentSize.height)
}
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
// Recalculate webView size
csWebViewHeight?.update(offset: 0)
webView.setNeedsLayout()
webView.layoutIfNeeded()
}
None of the listed JS funcs to get the content height worked reliably for me. What I found to consistently work is to find the last element in the DOM and get its position explicitly:
webView.evaluateJavaScript(
"document.body.lastChild.getBoundingClientRect().bottom + window.scrollY"
) { [weak self] (result, _) in
guard let self = self,
let height = result as? CGFloat,
height > 0 else { return }
self.heightConstraint?.constant = height
}

Resources