ios iOS检测截图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13484516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:15:21  来源:igfitidea点击:

iOS Detection of Screenshot?

iosscreenshot

提问by me2

The app Snapchat, on the App Store, is an app that lets you share pictures with a self-destruct on them. You can only view the pics for X seconds. If you attempt to take a screenshot while the picture is showing using the home-power key combo, it will tell the sender you tried to take a screenshot.

该应用程序Snapchat,在App Store上,是一个应用程序,可以让你分享他们自毁图片。您只能查看 X 秒的图片。如果您尝试在图片显示时使用 home-power 键组合截取屏幕截图,它会告诉发件人您尝试截取屏幕截图。

What part of the SDK lets you detect that the user is taking a screenshot? I did not know this was possible.

SDK 的哪一部分可以让您检测到用户正在截屏?我不知道这是可能的。

采纳答案by portforwardpodcast

I found the answer!! Taking a screenshot interrupts any touches that are on the screen. This is why snapchat requires holding to see the picture. Reference: http://tumblr.jeremyjohnstone.com/post/38503925370/how-to-detect-screenshots-on-ios-like-snapchat

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

回答by Mick MacCallum

As of iOS 7 the other answers are no longer true. Apple has made it so touchesCancelled:withEvent:is no longer called when the user takes a screenshot.

从 iOS 7 开始,其他答案不再正确。Apple 已经做到了,因此touchesCancelled:withEvent:当用户截取屏幕截图时不再调用它。

This would effectively break Snapchat entirely, so a couple betas in a new solution was added. Now, the solution is as simple as using NSNotificationCenter to add an observer to UIApplicationUserDidTakeScreenshotNotification.

这将有效地完全破坏 Snapchat,因此在新解决方案中添加了几个测试版。现在,解决方案就像使用 NSNotificationCenter 向UIApplicationUserDidTakeScreenshotNotification添加观察者一样简单。

Here's an example:

下面是一个例子:

Objective C

目标 C

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

Swift

迅速

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

回答by Esqarrouth

Heres how to do in Swift with closures:

以下是如何在 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")
}

Swift 4.2

斯威夫特 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()
    }
}

This is included as a standard function in:

这作为标准功能包含在:

https://github.com/goktugyil/EZSwiftExtensions

https://github.com/goktugyil/EZSwiftExtensions

Disclaimer: Its my repo

免责声明:这是我的回购

回答by Maksim Kniazev

Latest SWIFT 3:

最新的SWIFT 3

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

In viewDidLoad, call this function

viewDidLoad 中,调用这个函数

detectScreenShot { () -> () in
 print("User took a screen shot")
}

However,

然而,

NotificationCenter.default.addObserver(self, selector: #selector(test), name: .UIApplicationUserDidTakeScreenshot, object: nil)

    func test() {
    //do stuff here
    }

works totally fine. I don't see any points of mainQueue...

工作得很好。我没有看到 mainQueue 的任何点...

回答by Hamid Shahsavari

Swift 4+

斯威夫特 4+

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

by using this observer you can find out when user takes a screenshot, but you can not prevent him.

通过使用此观察者,您可以了解用户何时截屏,但您无法阻止他。

回答by iDev

Looks like there are no direct way to do this to detect if user has tapped on home + power button. As per this, it was possible earlier by using darwin notification, but it doesn't work any more. Since snapchat is already doing it, my guess is that they are checking the iPhone photo album to detect if there is a new picture got added in between this 10 seconds, and in someway they are comparing with the current image displayed. May be some image processing is done for this comparison. Just a thought, probably you can try to expand this to make it work. Check this for more details.

看起来没有直接的方法来检测用户是否点击了home + power button. 按照这个,可以更早地使用达尔文通知,但它不再起作用了。由于 snapchat 已经这样做了,我的猜测是他们正在检查 iPhone 相册以检测在这 10 秒之间是否添加了新图片,并以某种方式与显示的当前图像进行比较。可能是为了进行比较而进行了一些图像处理。只是一个想法,也许您可​​以尝试扩展它以使其工作。检查此了解更多详细信息

Edit:

编辑:

Looks like they might be detecting the UITouch cancel event(Screen capture cancels touches) and showing this error message to the user as per this blog: How to detect screenshots on iOS (like SnapChat)

看起来他们可能正在检测 UITouch 取消事件(屏幕截图取消触摸)并根据此博客向用户显示此错误消息:如何检测 iOS 上的屏幕截图(如 SnapChat)

In that case you can use – touchesCancelled:withEvent:method to sense the UITouch cancellation to detect this. You can remove the image in this delegate method and show an appropriate alert to the user.

在这种情况下,您可以使用– touchesCancelled:withEvent:方法来感知 UITouch 取消以检测到这一点。您可以在此委托方法中删除图像并向用户显示适当的警报。

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesCancelled:touches withEvent:event];

    NSLog(@"Touches cancelled");

    [self.imageView removeFromSuperView]; //and show an alert to the user
}

回答by Devbot10

Swift 4 Examples

Swift 4 示例

Example #1 using closure

Example #1 使用闭包

NotificationCenter.default.addObserver(forName: .UIApplicationUserDidTakeScreenshot, 
                                       object: nil, 
                                       queue: OperationQueue.main) { notification in
    print("\(notification) that a screenshot was taken!")
}

Example #2 with selector

Example #2 带选择器

NotificationCenter.default.addObserver(self, 
                                       selector: #selector(screenshotTaken), 
                                       name: .UIApplicationUserDidTakeScreenshot, 
                                       object: nil)

@objc func screenshotTaken() {
    print("Screenshot taken!")
}