iOS检测截图?

me2*_*me2 128 screenshot ios

App Store上的应用程序Snapchat是一款应用程序,可让您在其上分享带有自毁功能的图片.您只能查看X秒的照片.如果您在使用家用电源键组合显示图片时尝试截取屏幕截图,它会告诉发件人您尝试截取屏幕截图.

SDK的哪个部分可以让您检测到用户正在截取屏幕截图?我不知道这是可能的.

Mic*_*lum 350

从iOS 7开始,其他答案已不再适用.touchesCancelled:withEvent:当用户截取屏幕截图时,Apple已经不再调用了它.

这将完全打破Snapchat,因此增加了一个新解决方案中的几个beta.现在,解决方案就像使用NSNotificationCenter将观察者添加到UIApplicationUserDidTakeScreenshotNotification一样简单.

这是一个例子:

目标C.

NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
                                                  object:nil
                                                   queue:mainQueue
                                              usingBlock:^(NSNotification *note) {
                                                 // executes after screenshot
                                              }];
Run Code Online (Sandbox Code Playgroud)

迅速

NotificationCenter.default.addObserver(
    forName: UIApplication.userDidTakeScreenshotNotification,
    object: nil,
    queue: .main) { notification in
        //executes after screenshot
}
Run Code Online (Sandbox Code Playgroud)

  • 这不能阻止截屏.它只能让应用程序知道已经拍摄了一个.来自UIApplication类参考:UIApplicationUserDidTakeScreenshotNotification当用户按下Home和Lock按钮进行截屏时发布.此通知不包含userInfo字典.在截屏后发布此通知. (41认同)
  • +1很棒,现在需要接受这个答案. (11认同)
  • @badweasel正确.考虑到这个通知遵循传统的Cocoa命名约定,单词"Did"意味着它是在事后发布的.在这种情况下没有"Will"等效,并且AFAIK无法阻止用户使用公共API截取屏幕截图. (6认同)
  • 使用这个和`touchesCancelled:withEvent:`应该允许你检测所有(最近)iOS版本的屏幕截图. (3认同)

por*_*ast 22

我找到了答案!! 截屏会中断屏幕上的任何触摸.这就是为什么Snapchat需要持有才能看到图片.参考:http://tumblr.jeremyjohnstone.com/post/38503925370/how-to-detect-screenshots-on-ios-like-sn

  • iOS 7不再适用.有关iOS7 +解决方案,请参阅[下方](http://stackoverflow.com/a/18158483/384110). (15认同)
  • 乔说的是对的.Asker应该取消选中这个正确的答案. (6认同)

Esq*_*uth 11

继承人如何在Swift中使用闭包:

func detectScreenShot(action: () -> ()) {
    let mainQueue = NSOperationQueue.mainQueue()
    NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationUserDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
        // executes after screenshot
        action()
    }
}

detectScreenShot { () -> () in
    print("User took a screen shot")
}
Run Code Online (Sandbox Code Playgroud)

Swift 4.2

func detectScreenShot(action: @escaping () -> ()) {
    let mainQueue = OperationQueue.main
    NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: mainQueue) { notification in
        // executes after screenshot
        action()
    }
}
Run Code Online (Sandbox Code Playgroud)

这包含在以下标准函数中:

https://github.com/goktugyil/EZSwiftExtensions

免责声明:我的回购


Ham*_*ari 8

斯威夫特 4+

NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: OperationQueue.main) { notification in
           //you can do anything you want here. 
        }
Run Code Online (Sandbox Code Playgroud)

通过使用这个观察者,你可以发现用户何时截屏,但你无法阻止他。