成都网站建设设计

将想法与焦点和您一起共享

怎么在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中实现录屏和截屏,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注创新互联网站建设公司行业资讯频道。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享题目:怎么在iOS中实现录屏和截屏-创新互联
网址分享:http://chengdu.cdxwcx.cn/article/dpsjjd.html