Take a screenshot using the host application with the share / action extensions of iOS?

I would like to know how to take a screenshot using an iOS host application using the share / action extension.

My use case is as follows:

  • use the Safari browser to access the web page (https, e.g. gmail).
  • click the share button and select an extension
  • the extension will take a screenshot of the current web page

A working example for this use case is the Awesome Screenshot iOS app .

I tried the following methods

  • reload baseURI (loadRequest) in UIWebView / WKWebkit (will not be able to access https content, for example Gmail). So do not reboot (Awesome Screenshot does not reboot btw)
  • Used by ExtensionPreprocessingJS to retrieve the contents of the DOM through the arguments.completionFunction function. I could not extract document.body here, although I could get the source, etc. A LoadHTMLString with baseURI will ruin the presentation layer.
  • Used by html2canvas in ExtensionPreprocessingJS to retrieve the image and add it to the web page of the host application as a child, but does not know how to extract it. In addition, the image was lost for some web pages, such as Gmail.
  • Mobile Safari does not have the visibleContentsAsDataURL method.

I think a viable approach would be to use html2canvas in ExtensionPreprocessingJS, but how do I save this image?

+2
source share
1 answer

Edit: Thus, the following works in the simulator, but does not work on the device. I'm currently looking for a solution.

Here is a solution that in my opinion uses the Awesome Screenshot app:

func captureScreen() -> UIImage { // Get the "screenshot" view. let view = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false) // Add the screenshot view as a subview of the ShareViewController view. self.view.addSubview(view); // Now screenshot *this* view. UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0); self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true) let image: UIImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); // Finally, remove the subview. view.removeFromSuperview() return image } 
+1
source

Source: https://habr.com/ru/post/979258/


All Articles