36
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iOSでスクショ、画面録画時にトリガーする処理を実装する

Posted at

具体的にどういうことをしたいかというと、NetflixやAmazon Primeビデオなど動画配信サービスアプリ内でスクショをすると著作権、データ保護(DRM)のため、黒塗りされるのです。
(ちなみに画面録画すると、録画終了時に動画が保存されませんでしたとエラーが出ます)

Netflix Amazon Prime ビデオ
IMG_2281.PNG IMG_2283.PNG

UI周りのボタンはそのままですが、肝心の動画部分は黒塗りされています。
**これ、実装してみたい!**って思うじゃないですか。

ということで、調べてみると、結論から言うと、iOS12リリース時点では不可能なんですね。

なぜなら、スクショを撮った瞬間、画面録画がスタートされる瞬間を検知する仕組みがないからです。

ですが、スクショを撮った後、画面録画を終了する時点は検知する仕組みはあったので、紹介したいと思います。

###スクリーンショットを撮った後を検知する
使うのはuserDidTakeScreenshotNotificationです。

○userDidTakeScreenshotNotification - UIApplication | Apple Developer Documentation
https://developer.apple.com/documentation/uikit/uiapplication/1622966-userdidtakescreenshotnotificatio

NotificationCenterの通知を使って検知します。
これは何を検知するとかいうとUIScreen.main.isCapturedの値が変わった時に呼ばれます。

userDidTakeScreenshotNotificationはスクリーンショットを撮った時の通知です。

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(didTakeScreenCaptured(_:)),
        name: UIApplication.userDidTakeScreenshotNotification,
        object: nil
    )
}

@objc private func didTakeScreenCaptured(_ notification: Notification) {
    blackView.isHidden = UIScreen.main.isCaptured
}

上記はスクリーンショットを撮影後、隠しているビューを表示するというものです。

実際に撮影してみると、スクリーンショットはそのまま撮影できますが、その直後にメソッドが呼ばれて処理されます。

###画面収録した後を検知する
使うのはcapturedDidChangeNotification

○capturedDidChangeNotification - UIScreen | Apple Developer Documentation
https://developer.apple.com/documentation/uikit/uiscreen/2921652-captureddidchangenotification

使い方はuserDidTakeScreenshotNotificationと同じで、viewDidLoad(_:)などで実装します。

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(didCapturedScreen(_:)),
        name: UIScreen.capturedDidChangeNotification,
        object: nil
    )
}

@objc private func didCapturedScreen(_ notification: Notification) {
    yellowView.isHidden = UIScreen.main.isCaptured
}

こちらもUIScreen.main.isCapturedの値が変更になったら、呼ばれます。

簡単ですが、サンプルを作ってみました。
こちらからどうぞ。

○ScreenCapturedSample
https://github.com/takashings/ScreenCapturedSample

実際にどういう時に使うのか

正直userDidTakeScreenshotNotificationuserDidTakeScreenshotNotificationでは撮影後に呼ばれるため、何の意味もないのでは?という風に思いますよね。
はい、僕も同じく同様の感想です。

ですが、こういう仕組みがあるからには何か活用できるのではないかと思います。

考え得る使いどころとしては、スクショ、画面録画をしたら個人で楽しむ分には良いけど、ネットに流さないでねとアラートを表示したり、逆に**今スクショしたものをSNSにシェアしませんか?**と促す(特にゲームでは使えるかも?)という感じでしょうか。

将来的にはもしかしたら、userWillTakeScreenshotNotificationuserWillTakeScreenshotNotificationのようなものが実装されるかもしれませんね(されるといいな)。

36
24
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
36
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?