20

Is there any way to simulate taking a screen shot on iOS simulator (equivalent of home + power on device)? My goal is NOT to save (cmd + s) or copy a shot of screen (from simulator menu items) but to capture UIApplicationUserDidTakeScreenshotNotification event.

Edward Mehr
  • 651
  • 9
  • 21
  • could you describe more about it. what you tried! – nisar Jan 29 '15 at 09:51
  • Updated the answer. I have already tried saving the screen shot or copying the scree from simulator menu but none of them triggered the handler for UIApplucationUserDidTakeScreenshotNotification. – Edward Mehr Jan 29 '15 at 10:08
  • could you refer this link [link] (http://stackoverflow.com/questions/13484516/ios-detection-of-screenshot) – nisar Jan 29 '15 at 11:03

3 Answers3

16

Use LLDB to mimic the screenshot NSNotification:

(lldb) expr [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]

Pass --ignore-breakpoints false -- to the expr command if you want to stop at a breakpoint too:

(lldb) expr --ignore-breakpoints false -- [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]
Alan Zeino
  • 4,406
  • 2
  • 23
  • 30
  • Here is steps for programmatically invoke `UIApplicationUserDidTakeScreenshotNotification`. 1. When Xcode debugging click the `Pause` Button (It between the `Deactivate breakpoint` button and `Step over` button). 2. The app will the enter paused states and shows `(lldb)` prompt (like stop at breakpoint). 3. Then, enter the that command `expr [[NSNotificationCenter defaultCenter] postNotificationName:(NSNotificationName)UIApplicationUserDidTakeScreenshotNotification object:nil]` 4. Don't forgot to press play button to continue process running. – Johnny Sep 03 '19 at 15:14
  • I could not get this to work in latest Xcode 11 / iOS 13. – BaseZen Mar 14 '20 at 03:41
4

No, this is not currently possible in the iOS Simulator.

Jeremy Huddleston Sequoia
  • 22,938
  • 5
  • 78
  • 86
0

I too not able to detect UIApplicationUserDidTakeScreenshotNotification, But why dont you use this code to take screen shots and detect them using conditions.

//    // TAKE SCREENSHOT
CGRect myRect = [self.view bounds];
UIGraphicsBeginImageContext(myRect.size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[UIColor blackColor] set];
CGContextFillRect(ctx, myRect);
[self.view.layer renderInContext:ctx];
UIImage *viewimage = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(viewimage, 1.0);
if(imageData!=NULL)
{
   NSLog(@"user saved the image");
//Here you can detect the screen shots
   [self.imagevieww setImage:[UIImage imageWithData:imageData]];
}
else
{
   NSLog(@"user dont want to save image");
}
UIGraphicsEndImageContext();
nisar
  • 1,055
  • 2
  • 11
  • 26