LoginSignup
2
0

More than 3 years have passed since last update.

iOSでスクショと録画をフックしてみる(過去記事の再投稿)

Last updated at Posted at 2019-12-03

どうやらスクショや録画をフックできるらしいのでやってみた

スクショは取った後しかフックできない

UIApplicationUserDidTakeScreenshotNotification


    UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-100,self.view.frame.size.height/2-100,200,200)];

    label.backgroundColor = [UIColor redColor];
    label.text = @"スクショ待ち";
    label.textAlignment = NSTextAlignmentCenter;

    [self.view addSubview:label];

    // いろんなイベントをフックできる、今回スクショを登録
    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
                                                           //
                                                           label.text = @"スクショした";
                                                       }];

スクショ前
IMG_5393.PNG

スクショ後
IMG_5394.PNG

使い所

Twitterや画像、動画をみれるサービスなどでスクショしたことを投稿主に伝えたりスクショした人に警告を出したりできる。

ユーザーに不親切でもいいなら、スクショ後に数秒置いて写真アプリの最後の画像を消せばスクショの画像を消すことができる(オススメしないが)


キャプチャ

録画のイベントをフックできます。
ちなみに、Macを通してのQuickTimeでも反応します。

UIScreenCapturedDidChangeNotification

- (void)viewDidLoad {
    [super viewDidLoad];
    // 画像のロード
    UIGraphicsBeginImageContext(self.view.frame.size);
    [[UIImage imageNamed:@"rec.png"] drawInRect:self.view.bounds];
    self.recImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    // 録画イベントの感知
    [[NSNotificationCenter defaultCenter] addObserverForName:UIScreenCapturedDidChangeNotification
                                                      object:nil
                                                       queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
                                                           // 録画イベント変更
                                                           [self captureStatusDidChange];
                                                       }];
}
- (void)captureStatusDidChange
{
    NSLog(@"UIScreenCapturedDidChange");
    //録画中ならRec画像をだす
    if ( [[UIScreen mainScreen] isCaptured] ){
        self.view.backgroundColor = [UIColor colorWithPatternImage:self.recImage];
    }
    // 元に戻す
    else{
        self.view.backgroundColor = [UIColor whiteColor];
    }
}

動画
May-11-2018 11-40-28.gif

使い所

これを使えば、このゲーム名の名前を下に入れたり、URLを入れたりできる。
後画面を制御できるので全部を真っ黒にして録画禁止にするアプリを作ることができる。
(スクショと違って先に処理を実行できるため)


2
0
0

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
2
0