iOS 实现类似支付宝的进入后台模糊效果;截屏、录屏监控

WechatIMG95.jpeg

1、进入后台模糊效果:
在AppDelegate.swift中,

 lazy var visualEffectView: UIVisualEffectView = {
        let blur = UIBlurEffect.init(style: UIBlurEffect.Style.light)
        let view = UIVisualEffectView.init(effect: blur)
        view.frame = UIScreen.main.bounds
        return view
    }()
  func applicationWillResignActive(_ application: UIApplication) {
  
        window?.addSubview(self.visualEffectView)
    }
    func applicationDidBecomeActive(_ application: UIApplication) {
    
        self.visualEffectView.removeFromSuperview()
    }

2、截屏、录屏监控
在需要监控的ViewController里:

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        //检查是否“屏幕录制中”
        let screen = UIScreen.main
        if screen.isCaptured {
            self.screenshotAlert()
        }
    }
 override func viewDidLoad() {
        super.viewDidLoad()

 //添加“截屏”、“录屏”通知
        NotificationCenter.default.addObserver(self, selector: #selector(screenshotAlert), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(screenshotAlert), name: UIScreen.capturedDidChangeNotification, object: nil)
    }
   @objc func screenshotAlert() {
        let alert = UIAlertController.init(title: "提示", message: "[安全提醒]不要截图,录屏或分享给他人以保障账户安全。", preferredStyle: .alert)
        let cancleAction = UIAlertAction.init(title: "好的", style: .cancel, handler: nil)
        alert.addAction(cancleAction)
        self.present(alert, animated: true, completion: nil)
    }

你可能感兴趣的:(iOS 实现类似支付宝的进入后台模糊效果;截屏、录屏监控)