WKWebView screenshooting problems - ios

My iOS app is working with web content, and at certain time I need to take a screenshot of the web content. I'm using WKWebView (UIWebView was deprecated) for that purposes. All works fine, but recently I start to notice, that not all content appears on the screenshot. After some research, I've learned that some HTML pages have CANVAS tag, which is used for some real-time rendering/video playback. And this canvas content didn't appear on my screenshots. This quite frequently happens with ads.
I'm always using the following method for screenshot:
webView.drawHierarchy(in: rect, afterScreenUpdates: true)
At the moment, I can't find a way to take the screenshot as it appears on the screen.
Any ideas here are welcome since can't find any solutions.
Update:
Also, when trying to capture the view using Xcode debugger, it renders all the layers, view hierarchy etc.. Except that "canvas" view. It remains black.

Related

Action Extension with UI without covering whole screen?

I am currently trying trying to implement an Action Extension that transforms content on a Safari web page through Google's Translation API.
My issue is that I would like to display some UI on the screen without covering the whole screen, but no matter what I do that doesn't seem to work.
I have tried the following:
Setting this in the info.plist
<key>NSExtensionActionWantsFullScreenPresentation</key>
<false/>
Trying to change the content size in the interface builder to the following
https://imgur.com/a/gfYvFiD (Uploading images through stack isn't working for some reason)
Giving the UI a clear background color (always ends up being grey after the presentation is complete)
Setting the presentation style to be over current context.
Now i have seen a similar question where the accepted response was that it cannot be done due to Apple restrictions (https://stackoverflow.com/a/38764183/10058854), but I have seen some apps that seem to do it.
For example, iTranslate:
https://imgur.com/a/QpFo5St
The above 2 images clearly show that they have created an Action Extension and that the added UI is just the bar at the bottom of the web view.
Any help would be much appreciated, and thank you for reading this.

showing a pdf in uiwebview on ios9 sometimes scrolling is stuck

I'm working on an ios app written in swift, xcode7
A view has a UIWebView on it.
using loadRequest(NSURLRequest) we load a pdf file from disk.
This works fine, but sometimes the pdf file cannot be scrolled past the first page or what you see first on the screen. I can't pinpoint what causes the not-scrolling as sometimes I open the same file again and then it scrolls.
Trying to scroll when it doesn't want to bounces the bottom part of the screen and you can see there is another page, but you can't scroll it into view.
The UIScrollView contained inside the UIWebView (this is the default) knows there are more pages, because it shows "1 of 3" on screen.
The UIWebView
has 'scales page to fit' checked
has 'user interaction enabled' checked
has 'multiple touch' NOT checked
has 'autoresize subviews' checked
has mode 'Scale To Fill'
I have tried the following:
checking if the scrollview is not larger than the screen by placing a border around it: no, it fits on screen and is as large as you would expect
setting the internal UIScrollView.scollEnabled to true in code
In the application, when showing the UIWebview, it is resized on screen by collapsing an element above it, but I have tested this with fixed heights and it doesn't change anything.
When I call flashScrollIndicators() on the internal UIScrollView, this is consistent with the scrolling or non-scrolling. If you see the scrollbar flash, then scrolling is possible. If not, then you can't scroll past the first page.
Any other ideas I can try?
This sounds like a weird UIWebView problem.
Two additional things you can check :
Is the issue resolved after you rotate? (had this happen to me once)
If you call flashScrollIndicators on the internal UIScrollView and it's not responding. Is the internal UIScrollView a nil object?
Another solution is to try and use WKWebView. It might resolve the issue. Just be sure to only use it in the last versions of iOS.
You can also step down a layer and implement it with CoreGraphics since you have the PDF on the device itself and not on the web (official docs). There is probably a good CocoaPod or Carthage framework you can use for this.
The underlying problem was the fact that before loading a new pdf, the UIWebView was cleared each time by loading 'about:blank', which is the most suggested solution to clear the webview. However, when working with pdfs with different page sizes and different number of pages, this seems to mess with the built-in scrollview.
The solution in this case was to create a kind of 'blank' pdf with a single page (and maybe an application logo in it to distinguish it clearly for the user) and instead of loading about:blank, load this pdf instead before showing the webview to the user and while waiting for the real pdf to download to the device.

UICollectionView paging breaks after 2^24 pixels

The setup
I have a UICollectionView that allows the user to page through pictures, 12 to a page. There are upwards of 200,000 pictures that should all be available in the app. I'm not expecting a user to scroll to page 20,000, so there's function to jump to a certain page.
The problem
In landscape orientation, paging breaks down on page 16,385. The collection no longer adjusts to the page boundary. If you go back before page 16,384, you can get it to start working again, but no page past 16,385 works.
My delegate also stops getting scrollViewDidEndDecelerating: the message when the bug shows up.
The hypothesis
Page 16,385 in landscape orientation happens to start at pixel 16,777,216 which happens to be 2^24. I think that there's something in UICollectionView or UIScrollView that breaks past 2^24.
Is this just an undocumented limit? Am I out of luck?
The example
I've uploaded a project that demonstrates the problem. Here's the relevant view controller. If you shake your iPad or the simulator, you'll be taken to page 16,384, one page before the bug shows up.
The snark
If you don't think a user should need to be able to go to page 20,000, that's cool. I don't think it's relevant to the question.
I'd say this is an undocumented limitation and would file a Radar bug report, attaching the sample project as evidence. If you are looking for an alternative method, you could try using a UIPageViewController with a collection view for each page. You can choose the swiping animation, rather than the default iBooks-esque animation, and replicate your sample project very closely.
You could just load say 10,000 pages and when the user jumps, load an appropriately different 10,000 pages.
I wonder whether you've tried using multiple sections? Is the limitation per section or for the entire CV?

iPad Ibooks Javascript Canvas DrawImage does not work

I have a below script, its working in my windows browser. but this code does not work in Epub ebook (in my iPad iBooks)
<canvas id="e" width="177" height="113"></canvas>
<script>
var canvas = document.getElementById("e");
var context = canvas.getContext("2d");
var myImg = new Image();
myImg.src = "../Images/box.jpg";
myImg.onload = function() {
context.drawImage(myImg, 0, 0);
};
</script>
Yes, that’s correct. It seems iBooks supports a lot of canvas, but not the drawImage function. I submitted this to Apple a month or two ago and have yet to receive a response as to whether this is a bug or expected behavior. It’s possible there’s some security reason this doesn’t work, but that would seem odd considering it does in mobile Safari.
First, a question to Derrick Schultz. How did you submit a bug report to Apple?
Well, here's my own findings.
Platform: iPad iBooks. EPUB. Javascript.
Canvas. drawImage() function.
Does not work. Not even when the page is "refreshed" (or re-paginated? By rotating the iPad). This solution is a complete no-go.
A previous solution now seems to be the only partial workaround.
So, yes. Canvas and drawImage() does not work in iPad.
Hidden elements, rendered onscreen first
I created elements for all the images I wanted to display via interactivity (Javascript).
Critical step. I ensured that all images are rendered (visible) onscreen first. I had to do a setTimeout() for this.
Question: What is the iBooks event that lets me know all images are rendered onscreen? windows.onLoad doesn't work. Even onLoad for all the individual images doesn't work.
I then hid (display: none) all images, and displayed them upon user input.
Works.
The only problem is that big pictures that don't all fit onscreen (because I have to display them all first) won't work. They are not rendered onscreen. You can verify this theory by turning on Scrolling View on iBooks. When Scrolling View is on, all images, no matter how big, can be rendered onscreen.
PS: Wow. Stackoverflow does not allow comments (to seek clarification) on answers (need 50 reputation points). But it allows me to post an answer?

How to replace the "checkered" background shown during orientation changing in Mobile Safari (iPad)?

I am developing a small web application optimized for iPad (using mainly javascript & CSS quirks and hacks). However, there is an issue that I just can not find or come up how to resolve.
The thing is that if I change the orientation of the iPad, during the rotation animation a transparent ("checkered") background is shown in the places that were not rendered in previous orientation. One guy on forums.macrumors.com posted an excellent screenshot illustrating this behavior. The thing is, since my application has a rather dark look and & feel, this checkered background showing during the rotation seems to be awkwardly out of place.
However, this guy's screenshot was for the iPhone - that said, and considering that stackoverflow.com is more programming-focused, I thought I'd ask if anyone struggled with and, hopefully, successfully resolved this issue.
Safari displays the checkered background when the rendering can't keep up (try scrolling really fast). As far as I know there is no way of changing the pattern.

Resources