title: IOS iMessage extension screenshot detection
body: <p><em>Please use Swift 4+</em></p>
  
  <p><strong>NOTE: I am detecting the screenshot while I am within the iMessage extension, not in the standard iMessage view.</strong></p>
  
  <p>Update - I came up with a working solution that checks the photo library during the sensitive information period every .3 seconds or so to check if a new screenshot has been added. If the user does not give permission to the photo library, it won't show them the content until they enable it. However, I am still looking for other creative solutions that don't necessarily involve such a tedious process.</p>
  
  <p>I have an iMessage extension and I am trying to detect screenshots. I have tried every observer I have found online and for some reason it is not registering screenshots. </p>
  
  <p>ViewWillAppear()</p>
  
  <pre><code>UIScreen.main.addObserver(self, forKeyPath: "captured", options: .new, context: nil)
  </code></pre>
  
  <p>Observer</p>
  
  <pre><code>override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey: Any]?, context: UnsafeMutableRawPointer?) {
      if (keyPath == "captured") {
          let isCaptured = UIScreen.main.isCaptured
          print(isCaptured)
          screenshot()
          //screenshot() sends a message alerting the message was screens hotted. However, the print statement didn't even run.
      }
  }
  </code></pre>
  
  <p>ViewWillDisappear()</p>
  
  <pre><code>UIScreen.main.removeObserver(self, forKeyPath: "captured", context: nil)
  </code></pre>
  
  <p>I have also tried the standard default Notification Center</p>
  
  <pre><code>let mainQueue = OperationQueue.main
      NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
          // executes after screenshot
          print("Screenshotted")
          self.screenshot()
      }
  </code></pre>
  
  <p>For people who claim it is not possible to detect screenshots within an iMessage extension because it is an extension and not a full app, this developer has been able to successfully do it
  <a href="https://www.addictivetips.com/ios/get-a-screenshot-alert-for-pictures-sent-over-imessage/" rel="noreferrer">Working Example</a></p>