成都创新互联网站制作重庆分公司

怎么在iOS中实现录屏和截屏

本篇文章为大家展示了怎么在iOS中实现录屏和截屏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

目前创新互联已为成百上千家的企业提供了网站建设、域名、网站空间网站运营、企业网站设计、泗县网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

截屏状态获取

编辑相册中最新照片的方法iOS8之后就已经失效,框架“Photos”也在iOS10之后失效。

搜索发现UIApplication中仅有用户截屏后的通知,应用中只会收到已经截屏的通知并没办法干预。

// This notification is posted after the user takes a screenshot (for example by pressing both the home and lock screen buttons)
UIKIT_EXTERN NSNotificationName const UIApplicationUserDidTakeScreenshotNotification NS_AVAILABLE_IOS(7_0);

虽然无法直接干预,但可以知道用户截屏了就可以用其它的方式来限制用户的行为或者弹出提示告诉用户。

-(void)viewDidAppear:(BOOL)animated{
  [super viewDidAppear:animated];
  
  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenshots) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

-(void)screenshots
{
  UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"[安全提醒]内含个人资金账户。不要截图,录制或分享给他人以保障资金账户安全。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
  [alert1 show];

-(void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

录屏状态获取

iOS 11 SDK 中新增了UIScreen的API用以告知应用当前屏幕正在录屏。当UIScreen.isCaptured 为true时,表示当前屏幕正在被录制、镜像或被Airplay 发送。

当录屏状态发生变化时,UIKit会发送UIScreenCapturedDidChange的notification。

基于此,我们可以在应用中接收此通知,来对用户的录屏行为做相应的处理

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];

//监测当前设备是否处于录屏状态
  UIScreen * sc = [UIScreen mainScreen];
  if (@available(iOS 11.0, *)) {
    if (sc.isCaptured) {
      NSLog(@"正在录制~~~~~~~~~%d",sc.isCaptured);
      [self screenshots];
    }
  } else {
    // Fallback on earlier versions
  }
  if (@available(iOS 11.0, *)) {
//检测到当前设备录屏状态发生变化
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(screenshots) name:UIScreenCapturedDidChangeNotification object:nil];
  } else {
    // Fallback on earlier versions
  }
}

-(void) screenshots
{
  UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:nil message:@"[安全提醒]内含个人资金账户。不要截图,录制或分享给他人以保障资金账户安全。" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
  [alert1 show];

-(void)dealloc
{
  if (@available(iOS 11.0, *)) {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIScreenCapturedDidChangeNotification object:nil];
  } else {
    // Fallback on earlier versions
  }
}

上述内容就是怎么在iOS中实现录屏和截屏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联行业资讯频道。


当前名称:怎么在iOS中实现录屏和截屏
地址分享:http://cxhlcq.com/article/jgidjd.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部