I have a local template and generate a complete html string by inserting result of a web API call into the template. The html string has some links to external images.
I'm showing the string in a UIWebView by calling its loadHTMLString method as below ...
self.newsBodyWebView.loadHTMLString(unifiedDoc, baseURL: nil)
I'm not quite clear about loadHTMLString's behavior, it appears that it's rendering contents synchronously, like even if I'm sure that the text part has already returned, the UIWebView won't show any content (I'm seeing a blank white view) until the external images are downloaded completely.
Could you please suggest what I can do in this scenario? I'm thinking if I should put different sections of text and images into different UITableViewCell to separate their display, this does not seem like a tidy solution though.
Related
I'm using WKWebView to load a local html file using
[self.webView loadFileURL:tmpUrl allowingReadAccessToURL:directoryUrl];
Currently the webView will keep white screen for a long time, and once it shows content, all images were already downloaded.
It looks like the WKWebView will only render after all images downloaded.
Sample HTML content is here: https://pastebin.com/681btcSa
Then I did some test for pages without images. It seems that when loading complex HTML, WKWebView always takes a long time before render anything on screen.
I tried to call _updateVisibleContentRects and setNeedsLayout, but it helps little.
how to block all images in UIWebview like as if you are open
yahoo.com in this website all image content should be block mens user
can not see image.
thanks.
You could run or install some JavaScript which traverses the DOM and hides all images. Or add CSS for image tags to hide them. Or deny any image URL request in the web view delegate. There are a number of possible solutions depending on how the images are loaded and how you can tell what an image actually is.
After I loaded ppt on UIWebView, in
func webViewDidFinishLoad(webView: UIWebView) {
let html = webView.stringByEvaluatingJavaScriptFromString("document.documentElement.outerHTML")
}
I got the whole HTML for the ppt file.
But after I loaded .key or .pdf, the HTML is empty! So how are them loaded on UIWebView. Anyway I could get or set the content after I loaded them like ppt?
When you load a PDF document using UIWebView, you see in essence an image of the pages laid out in a scroll view. There is no HTML involved.
You ask about the "content" of the documents, but that does not really have a meaning in PDF. Think of a PDF documents as a set of instructions for drawing shapes, images and glyphs onto the screen.
As far as PDF is concerned, there is no such thing as a word, a paragraph, a heading and so on. And therefore, there isn't really such a thing as "content" in the same way as there is in an HTML page.
There are some libraries that try to recover structure from PDF, and the UIWebView uses one under the hood, at least to discover lines, words, paragraphs and columns, buy it is not accessible from the API, and therefore you cannot use it.
I have a native iOS app that contains a tab bar. The view controller for each tab contains a UIWebView. When the user switches between tabs, I load the HTML in the corresponding web view. The HTML is fully cached on a device. Here is how I feed the HTML to UIWebView:
[self.webView loadHTMLString:htmlString baseURL:baseUrl];
baseUrl is a file URL pointing to a directory where all assets are located.
This works great in online and offline modes, however it takes time for the UIWebView to parse and render the HTML. As a result, the user sees a brief blink of a white background when switching between tabs. I'd like to remove it, because the user is able to tell that the UI is not native (native UI renders instantly).
I was thinking about taking a screenshot of the UIWebView once it's done rendering the HTML and caching it in memory. The next time the user navigates to that web view, the app displays the screenshot while the UIWebView is rendering the HTML in the background. Finally, the app swaps the screen shot with the actual UIWebView and takes a new screenshot. This is similar to how Google Chrome app works.
Does anyone know a better solution to this problem?
Depends on how you present that web view. I'm not really sure from your description how you do it, but since you're talking about a blink of white background, I'm going to assume there is a situation where you have a web view with a loaded html and then you switch it to another html. The solution for that case would be:
create a second web view,
load the new html to this new web view,
when that web view finishes loading html (you can find out if you use UIWebViewDelegate) you can then quickly switch it with your old web view (meaning you finally add the new web view to the view hierarchy at THIS point, not when you created it).
This way you'll have an instant switch between old and new html, however user will be left waiting and it is your decision what to do with it. You could use UIActivityIndicatorView for example.
When webViewDidFinishLoad: gets called you can do any number of things, make the webview visible, remove something that you had on top of the webview etc...
The following code
<a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=156093464&id=156093462&s=143441">
<img height="15" width="61" alt="Randy Newman - Toy Story
- You've Got a Friend In Me" src="http://ax.phobos.apple.com.edgesuite.net/images/
badgeitunes61x15dark.gif"></img>
</a>
Is from apple's documentation
http://developer.apple.com/library/ios/#featuredarticles/iPhoneURLScheme_Reference/Articles/iTunesLinks.html#//apple_ref/doc/uid/TP40007896-SW1
When a html page containing that href is loaded into a browser the image is displayed, however when its loaded into a UIWebView it isn't displayed.
ShouldStartLoadWithRequest: does not get called for the following URL
http://ax.phobos.apple.com.edgesuite.net/images/
badgeitunes61x15dark.gif
Why does it not get called and why does this work in a browser but not in UIWebView?
Well, I just built a prototype with your HTML, and it worked fine for me.
However, a few thoughts:
First, if you implement webView:shouldStartLoadWithRequest:navigationType: in your web view delegate, don't expect a callback for every single item in your HTML page. If I remember correctly, it only calls that for top level requests (e.g. the whole page). So, I would not expect to get a callback just for that one img resource.
Since we already established that implementing webView:shouldStartLoadWithRequest:navigationType: won't do what you want, you might want to remove it altogether. When I first lazily put it in my code, I forgot to return YES; from that method. Since Objective-C is a rather loose language, the app built and ran, but obviously took NO to be the implicit return value from that method. NO stops the request from loading. If the image was the only thing in a sample page you had, you wouldn't see anything (and maybe falsely think it was just because of the img itself).
I can't tell if this is only in the HTML that you posted in your question, or if it's in the real code, too. But, you have whitespace inside your img src URL, after "images/". That would obviously make this break.
In my test, I also started with a web page that didn't scale content to fit. That image is rather small, so if you don't scale your web view to fit, and view it on an iPhone/iPod, you might not have noticed such a small image.
webview.scalesPageToFit = YES;
Anyway, that's all I can think of. Like I said, it works for me. It's not the image itself, or any difference between Mobile Safari, vs. UIWebView.
Double check and report back :)