Take screenshot of host app using iOS share/action extensions?
Asked Answered
C

1

2

I will like to know how to take a screenshot of the iOS host app with the use of a share/action extension.

My use case is as follows:

  1. use the Safari browser to access a webpage (https such as gmail)
  2. tap on the Share button and select the extension
  3. the extension will take a screenshot of the current webpage

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

I had tried the following methods

  1. reload the baseURI (loadRequest) on a UIWebView/WKWebkit (would not be able to access https contents such as Gmail). So not doing any reload (Awesome Screenshot is not doing a reload btw)
  2. Used the ExtensionPreprocessingJS to obtain the DOM contents through the arguments.completionFunction function. I could not extract the document.body here although i could get the source etc. loadHTMLString with the baseURI will mess up the presentation layer.
  3. Used html2canvas in the ExtensionPreprocessingJS to obtain an image and appended it to the host app's webpage as a child but do not know how to extract it. Also, the image got lost for some webpages such as Gmail.
  4. Mobile Safari does not have the visibleContentsAsDataURL method.

I think a viable approach will be to use the html2canvas in the ExtensionPreprocessingJS but how do I save this image somehow?

Considering answered 14/10, 2015 at 7:56 Comment(4)
did you solve this??Imperious
@Andres, not so ... what's your idea?Considering
so far I have no idea! I'll post the answer If I find out how to do it! :)Imperious
@Andres, noted with thanks!Considering
C
2

Edit: So the below works in the Simulator but does not work on the device. I'm presently looking for a solution as well.

Here's a solution that I think the Awesome Screenshot app uses:

func captureScreen() -> UIImage
{
    // Get the "screenshot" view.
    let view = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)

    // Add the screenshot view as a subview of the ShareViewController's 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
}
Columbian answered 22/12, 2015 at 6:31 Comment(1)
thanks for your idea! From what I gathered, the focus should be on the preprocessing JS which is responsible for grabbing all the content for display.Considering

© 2022 - 2024 — McMap. All rights reserved.