这个在应用商店上的Snapchat应用程序可以让你分享带有自毁功能的图片。你只能在X秒内查看这些照片。如果你试图使用home-power键组合键在显示图片时截屏,它会告诉发送者你尝试截图了。
SDK的哪个部分让你检测用户正在截屏?我不知道这是可能的。
这个在应用商店上的Snapchat应用程序可以让你分享带有自毁功能的图片。你只能在X秒内查看这些照片。如果你试图使用home-power键组合键在显示图片时截屏,它会告诉发送者你尝试截图了。
SDK的哪个部分让你检测用户正在截屏?我不知道这是可能的。
iOS 7及以后版本已不再调用其他答案中所述的touchesCancelled: withEvent:
,当用户截屏时。这将导致Snapchat无法正常运行,因此添加了一些新的解决方案。现在,解决方案很简单,只需使用NSNotificationCenter将观察者添加到UIApplicationUserDidTakeScreenshotNotification即可。
以下是示例:
NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationUserDidTakeScreenshotNotification
object:nil
queue:mainQueue
usingBlock:^(NSNotification *note) {
// executes after screenshot
}];
NotificationCenter.default.addObserver(
forName: UIApplication.userDidTakeScreenshotNotification,
object: nil,
queue: .main) { notification in
//executes after screenshot
}
touchesCancelled:withEvent:
可以让你在所有(较新的)iOS版本中检测到截屏。 - Joshua Gross我找到了答案!! 截屏会打断屏幕上正在进行的所有触摸操作。这就是为什么Snapchat需要按住才能查看图片的原因。 参考资料: http://tumblr.jeremyjohnstone.com/post/38503925370/how-to-detect-screenshots-on-ios-like-snapchat
以下是使用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
->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()
}
}
这已经作为标准函数包含在:
https://github.com/goktugyil/EZSwiftExtensions
免责声明:这是我的存储库
Swift 4+
NotificationCenter.default.addObserver(forName: UIApplication.userDidTakeScreenshotNotification, object: nil, queue: OperationQueue.main) { notification in
//you can do anything you want here.
}
通过使用这个观察器,您可以找出用户何时截屏,但不能阻止他。
最新的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()
}
}
detectScreenShot { () -> () in
print("User took a screen shot")
}
NotificationCenter.default.addObserver(self, selector: #selector(test), name: .UIApplicationUserDidTakeScreenshot, object: nil)
func test() {
//do stuff here
}
正常工作。我看不出使用mainQueue的任何好处...
看起来没有直接的方法来检测用户是否按下了Home + 电源按钮
。根据this所述,早期可以使用达尔文通知实现,但现在不再起作用。由于Snapchat已经这样做了,我的猜测是他们正在检查iPhone相册,以检测是否在这10秒钟内添加了新图片,并以某种方式与当前显示的图像进行比较。也许进行了一些图像处理来进行比较。只是一个想法,也许你可以尝试扩展它以使其工作。请查看此处以获取更多详细信息。
编辑:
看起来他们可能正在检测UITouch取消事件(屏幕截图取消触摸),并根据此博客向用户显示此错误消息:iOS上如何检测屏幕截图(如SnapChat)
在这种情况下,您可以使用 - 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
}
touchesEnded:withEvent
(或类似的回调),因此他们可以监视这种独特的事件模式?我可能完全错了,但这是我目前唯一的理论。 - smileyborgSwift 4 示例
示例 #1 使用闭包
NotificationCenter.default.addObserver(forName: .UIApplicationUserDidTakeScreenshot,
object: nil,
queue: OperationQueue.main) { notification in
print("\(notification) that a screenshot was taken!")
}
带选择器的示例#2
NotificationCenter.default.addObserver(self,
selector: #selector(screenshotTaken),
name: .UIApplicationUserDidTakeScreenshot,
object: nil)
@objc func screenshotTaken() {
print("Screenshot taken!")
}