0

I am trying to receive screenshot data from my Share Extension. I am running iOS 15.5.

override func didSelectPost() {
   if let extensionItems = self.extensionContext?.inputItems as? [NSExtensionItem]  {
      let attachments     = extensionItems.first?.attachments ?? []
      let imageType       = UTType.image.identifier
      
      for provider in attachments {
         if provider.hasItemConformingToTypeIdentifier(imageType) {
            print("It is an image")

            // this seems only to handle media from photos
            provider.loadFileRepresentation(forTypeIdentifier: imageType) { (unsafeFileUrl, error) in
               print("We have the image")
            }
         }
      }
   }
}

Observed behaviour

  • When sharing a photo from the Photos App, the didSelectPost() method works as expected.
  • When capturing an image from the Share button, I can see the print("It is an image"), however I cannot access the actual image data.

What I have tried

  • I have tried to access the provider's content using loadItem and loadDataRepresentation, neither trigger a print statement.
  • Other StackOverflow question suggest using

Questions that have not resolved my issue

Expected behaviour

  • I get the Screen Shot image data which I can save to disc.

Queries

  • Why can I not access the Screen Shot data?
  • How do I access the screen shot data?
airs
  • 3
  • 3

2 Answers2

0

I had the same issue. The data from the screenshot is actually a UIImage, so this worked for me:


                         //sometimes the item is a UIImage (i.e., screenshots)
                         itemProvider.loadItem(forTypeIdentifier: UTType.image.identifier) { item, error in
                             if let image = item as? UIImage {
                                 DispatchQueue.main.async { [weak self] in
                                     guard let self = self else { return }
                                     self.imageView.image =   image
                                 }
                                 return
                             }

                         }
Buyin Brian
  • 2,781
  • 2
  • 28
  • 48
  • Thanks for taking the time to comment. I've just tried the code and for some reason the "loadItem" closure isn't executing..! Works absolutely fine if I send a URL, which I can load with .loadFileRepresentation. This has been killing me for weeks. – airs Aug 30 '22 at 20:09
  • I'm having the same problem, it doesn't get to the part of the code where it parses the data received. I already tried with breakpoints, but it seems like it's not even loading the extension whenever I try to call it from the screenshot view. Btw, I'm using iOS 16... – Rafael Bitencourt Dec 04 '22 at 03:42
  • If `loadItem(forTypeIdentifier:)` is not being called (after taking a screenshot), then you need to make sure you're calling `completeRequest(returningItems:completionHandler:)` at the right time. This answer explains it in detail: https://stackoverflow.com/questions/24235954/ios-8-share-extension-loaditemfortypeidentifieroptionscompletionhandler-compl/31238670#31238670 – chockenberry Jan 04 '23 at 22:46
0

Did you add the activation rules to the info.plist of the share sheet:

        <dict>
            <key>NSExtensionActivationRule</key>
            <dict>
                <key>NSExtensionActivationSupportsImageWithMaxCount</key>
                <integer>1</integer>
                <key>NSExtensionActivationSupportsText</key>
                <true/>
            </dict>
        </dict>
Buyin Brian
  • 2,781
  • 2
  • 28
  • 48
  • I have the following activation rule, so I can capture URLs and images: `SUBQUERY ( extensionItems, $extensionItem, SUBQUERY ( $extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.image" ).@count == 1 ).@count == 1 || SUBQUERY ( extensionItems, $extensionItem, SUBQUERY ( $extensionItem.attachments, $attachment, ANY $attachment.registeredTypeIdentifiers UTI-CONFORMS-TO "public.url" ).@count == 1 ).@count == 1` – airs Sep 01 '22 at 08:15
  • Sorry, formatting isn't great on comments! – airs Sep 01 '22 at 08:17