Problem in Xcode 8 and iOS 10 to determine the height of a web view

Hello,


I want to show expanded more information (content from a html file) when man clicks on a button.

when man clicks on the button again, the more information is hidden.

The more information is shown in a web view.

After the content of the web view is loaded, the height of web view is set. So that the more inforamtion is shown rightly.


Some of the code are:

NSLogI(@" path %@", path); // e.g.Path is "...Library/Developer/CoreSimulator/Devices/4074CF50-3416-4D41-8FF6-BD9D9A063512/data/Containers/Bundle/Application/293CCA8A-A184-4232-8E7E-55F4FF7E218F/***.app/help.html"

NSMutableURLRequest* req = [[NSMutableURLRequest alloc] init];

req.URL = [NSURL fileURLWithPath:path isDirectory: NO];

req.cachePolicy = NSURLRequestReloadIgnoringCacheData;

req.timeoutInterval = 1;

[self.webView loadRequest:req];


- (void)webViewDidFinishLoad:(UIWebView *)webView {

double webViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];

self.webViewHeight = webViewHeight;

self.btnShowHelp.hidden = NO;

}


If I use xcode 7.3.1 to run the code, it works well on devices and simulators on iOS which is less than iOS 10.


If I use Xcode 8 beta (8S128d) to run the same code, some content of the web view is cut off on iOS that is less than iOS 10,

if the content is a little bit longer. And No content is shown on devices and simulators on iOS 10, if I use Xcode 8 beta to run the code.


In the above method [[webView stringByEvaluatingJavaScriptFromString: @"document.height"] floatValue] is 0 on iOS 10 devices and simulators.


example content of help.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">/discussion/http:/ <p style=

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>xxxxx</title>

<link href="checklistStyle.css" rel="stylesheet" type="text/css" />

<script type="text/javascript" src="NativeBridge.js"></script>

</head>

<body>

<div id="content" class="checklist">

xxxxxxxxxxxxxxxxxxxxx

<p class="Headline1">xxxxxxxxxxxxxxxxxxxxx</p>

Tipp: yyyyyyyyyyyyyyyyyyyy

<p class="Text3"><strong>Tipp:</strong> yyyyyyyyyyyyyyyyyyyy</p>

zzzzzzzzzzz

<p class="Headline1">zzzzzzzzzzz</p>

<ol class="decimallist">

  • aaaaaaaa....
  • <li>aaaaaaaa....</li>

  • bbbbbbb..
  • <li>bbbbbbb..</li>

  • cccccccc..
  • <li>cccccccc..</li>

  • ddddddd....
  • <li>ddddddd....</li>

  • eeeeeeee...
  • <li>eeeeeeee...</li>

  • ffffffffffff....
  • <li>ffffffffffff....</li>

  • gggggggg".
  • <li>gggggggg".</li>

  • hhhh......
  • <li>hhhh......</li>

    </ol>

    wwwwwww ...........

    <p class="Headline1">wwwwwww ...........</p>

    <ol class="decimallist">

  • aaaaaaaa....
  • <li>aaaaaaaa....</li>

  • cccccccc..
  • <li>cccccccc..</li>

  • eeeeeeee...
  • <li>eeeeeeee...</li>

  • gggggggg".
  • <li>gggggggg".</li>

    </ol>

    mmmmmmm........

    <p class="Headline1">mmmmmmm........</p>

    <ol class="decimallist">

  • kkkkkk.....
  • <li>kkkkkk.....</li>

  • jjjjjjjjjj.
  • <li>jjjjjjjjjj.</li>

  • iiiiii
  • <li>iiiiii</li>

  • pppppppp
  • <li>pppppppp</li>

    </ol>

    <p> </p>

    </div>

    </body>

    </html>


    My Questions:

    1. [[webView stringByEvaluatingJavaScriptFromString: @"document.height"] floatValue] in iOS 10 returns 0. No content is shown on iOS.

    2. Using Xcode 8 beta 2 to run the code, the content of web view is cut off on iOS that is less than iOS 10, when the content is a little bit longer.

    But the same content can be shown right on iOS that is less than iOS 10 when the code is run from code 7.3.1.

    Are the above two bugs of iOS 10/ Xcode 8 beta 2?

    3. With Xcode 8 oder iOS 10 how can I get the right height of web view?


    I tried changing webViewDidFinishLoad:(UIWebView *)webView to the following:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {

    float height1 = [[webView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];

    float height2 = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];

    float height3 = webView.scrollView.contentSize.height;

    self.webViewHeight = MAX(height1, MAX(height2, height3));

    self.btnShowHelp.hidden = NO;

    }

    In iOS 10, height1 is 0, height 2 and height 3 have a value. but they are the right height. The content of the web view

    is cut off, if the content is a little bit longer, when the code is run from Xcode 8 beta 2.


    Thanks a lot

    Hi Wen,

    I'm experiencing the same problem with iOS 10 in both the Beta 1 and 2. I would suggest filing a Radar to get Apple's attention on this because ot seems to be an issue with the Javascript evaluation. I had the same problem trying to get any Javascript response with UIWebView. Ran inside the webViewDidFinishLoad:


    (lldb) po [webview stringByEvaluatingJavaScriptFromString:@"window;"]

    <object returned empty description>

    (lldb) po [webview stringByEvaluatingJavaScriptFromString:@"window.body"]

    <object returned empty description>

    (lldb) po [webview stringByEvaluatingJavaScriptFromString:@"window.document.getElementById('body').offsetHeight;"]

    <object returned empty description>


    However, I have noticed that the following JavaScriptFromString works:

    NSString *webViewHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

    //then convert it to a floatValue

    self.webViewHeight = [webViewHeight floatValue];


    I hope this helps.

    Adam Govan

    Did you find any resolution to this issue? How did you get on reporting the issue to Apple?

    Hi, we ran into this too and found the following to be working in iOS 10 beta 6:


    Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);


    I'm not sure if we are going to ship this, we still have to test it on all platfoms we're targeting.


    When researching ths, I found a note from Mozilla, that they are dropping document.height, maye WebKit did too...

    https://developer.mozilla.org/de/docs/Web/API/Document/height

    Thanks so much for your post - it pointed me in the right direction and saved me a lot of time.

    How to get height if there is image inside webview content?

    I have WKWebView inside the UITableViewCell. The web view load request and then after finished loading, I'll resize the web view height to be equal to its content height, then adjust table view cell height to fit accordingly.


    http://stackoverflow.com/questions/39549103/wkwebview-not-render-correctly-in-ios-10

    Thank you so much! It works on my code 🙂

    Problem in Xcode 8 and iOS 10 to determine the height of a web view
     
     
    Q