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

发布时间:2021-05-31 17:43:45 作者:Leah
来源:亿速云 阅读:418

本篇文章为大家展示了怎么在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中实现录屏和截屏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. IOS如何实现手机截屏
  2. ios截屏使用的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

ios

上一篇:怎么在iOS中使用tableView实现顶部图片拉伸效果

下一篇:怎么在iOS中利用push实现侧滑返回功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》