iOS 中捕获截屏操作

2018-05-11  本文已影响0人  咖啡绿茶1991

转自: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>}

附件:

UIApplicationUserDidTakeScreenshotNotification检查截屏并获取截屏后的图片

iOS开发-检测用户截屏, 并获取所截图片

上一篇 下一篇

猜你喜欢

热点阅读