iOS 中捕获截屏操作
转自:iOS知识小集
在iOS 7后,苹果提供了UIApplicationUserDidTakeScreenshotNotification通知来告诉App用户做了截屏操作。苹果的描述如下:
// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
UIKIT_EXTERN NSString *const UIApplicationUserDidTakeScreenshotNotification NS_AVAILABLE_IOS(7_0);
我们要使用这个通知,只需要在相应的界面或者AppDelegate中往通知中心添加该通知的观察者即可。例如我在AppDelegate中添加观察者为自己:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeScreenShot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
return YES;
}
然后在对应Selector 中处理逻辑:
- (void)takeScreenShot:(NSNotification *)notice
{
NSLog(@"截图");
// 这里其实可以从相册读取最新的一张图,那就是刚才的截屏图片
}
打印出来通知的信息如下:
2016-11-12 13:46:55.750 Demo[6376:2821166] 截图
Printing description of notice:
NSConcreteNotification 0x15668df0 {name = UIApplicationUserDidTakeScreenshotNotification; object = <UIApplication: 0x15555550>}