index.uts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import { NotificationCenter } from 'Foundation';
  2. import { CGRect } from "CoreFoundation";
  3. import { UIApplication, UIView, UITextField, UIScreen, UIDevice } from "UIKit"
  4. import { UTSiOS } from "DCloudUTSFoundation"
  5. import { DispatchQueue } from 'Dispatch';
  6. import { SetUserCaptureScreenOptions, OnUserCaptureScreenCallbackResult, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen, UserCaptureScreenCallback, SetUserCaptureScreenSuccess } from "../interface.uts"
  7. import { SetUserCaptureScreenFailImpl } from "../unierror.uts"
  8. /**
  9. * 定义监听截屏事件工具类
  10. */
  11. class CaptureScreenTool {
  12. static listener : UserCaptureScreenCallback | null;
  13. static secureView : UIView | null;
  14. // 监听截屏
  15. static listenCaptureScreen(callback : UserCaptureScreenCallback | null) {
  16. this.listener = callback
  17. // 注册监听截屏事件及回调方法
  18. // target-action 回调方法需要通过 Selector("方法名") 构建
  19. const method = Selector("userDidTakeScreenshot")
  20. NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.userDidTakeScreenshotNotification, object = null)
  21. }
  22. // 捕获截屏回调的方法
  23. // target-action 的方法前需要添加 @objc 前缀
  24. @objc static userDidTakeScreenshot() {
  25. // 回调
  26. const res: OnUserCaptureScreenCallbackResult = {
  27. }
  28. this.listener?.(res)
  29. }
  30. // 移除监听事件
  31. static removeListen(callback : UserCaptureScreenCallback | null) {
  32. this.listener = null
  33. NotificationCenter.default.removeObserver(this)
  34. }
  35. static createSecureView() : UIView | null {
  36. let field = new UITextField(frame = CGRect.zero)
  37. field.isSecureTextEntry = true
  38. if (field.subviews.length > 0 && UIDevice.current.systemVersion != '15.1') {
  39. let view = field.subviews[0]
  40. view.subviews.forEach((item) => {
  41. item.removeFromSuperview()
  42. })
  43. view.isUserInteractionEnabled = true
  44. return view
  45. }
  46. return null
  47. }
  48. // 开启防截屏
  49. static onAntiScreenshot(option : SetUserCaptureScreenOptions) {
  50. // uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
  51. DispatchQueue.main.async(execute = () : void => {
  52. let secureView = this.createSecureView()
  53. let window = UTSiOS.getKeyWindow()
  54. let rootView = window.rootViewController == null ? null : window.rootViewController!.view
  55. if (secureView != null && rootView != null) {
  56. let rootSuperview = rootView!.superview
  57. if (rootSuperview != null) {
  58. this.secureView = secureView
  59. rootSuperview!.addSubview(secureView!)
  60. rootView!.removeFromSuperview()
  61. secureView!.addSubview(rootView!)
  62. let rect = rootView!.frame
  63. secureView!.frame = UIScreen.main.bounds
  64. rootView!.frame = rect
  65. }
  66. }
  67. let res: SetUserCaptureScreenSuccess = {
  68. }
  69. option.success?.(res)
  70. option.complete?.(res)
  71. })
  72. }
  73. // 关闭防截屏
  74. static offAntiScreenshot(option : SetUserCaptureScreenOptions) {
  75. DispatchQueue.main.async(execute = () : void => {
  76. if (this.secureView != null) {
  77. let window = UTSiOS.getKeyWindow()
  78. let rootView = window.rootViewController == null ? null : window.rootViewController!.view
  79. if (rootView != null && this.secureView!.superview != null) {
  80. let rootSuperview = this.secureView!.superview
  81. if (rootSuperview != null) {
  82. rootSuperview!.addSubview(rootView!)
  83. this.secureView!.removeFromSuperview()
  84. }
  85. }
  86. this.secureView = null
  87. }
  88. let res: SetUserCaptureScreenSuccess = {
  89. }
  90. option.success?.(res)
  91. option.complete?.(res)
  92. })
  93. }
  94. }
  95. /**
  96. * 开启截图监听
  97. */
  98. export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
  99. CaptureScreenTool.listenCaptureScreen(callback)
  100. }
  101. /**
  102. * 关闭截屏监听
  103. */
  104. export const offUserCaptureScreen : OffUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
  105. CaptureScreenTool.removeListen(callback)
  106. }
  107. /**
  108. * 开启/关闭防截屏
  109. */
  110. export const setUserCaptureScreen : SetUserCaptureScreen = function (options : SetUserCaptureScreenOptions) {
  111. if (UIDevice.current.systemVersion < "13.0") {
  112. let res = new SetUserCaptureScreenFailImpl(12001)
  113. options.fail?.(res);
  114. options.complete?.(res);
  115. } else if (UIDevice.current.systemVersion == "15.1") {
  116. let res = new SetUserCaptureScreenFailImpl(12010)
  117. options.fail?.(res);
  118. options.complete?.(res);
  119. } else {
  120. if (options.enable == true) {
  121. CaptureScreenTool.offAntiScreenshot(options)
  122. } else {
  123. CaptureScreenTool.onAntiScreenshot(options)
  124. }
  125. }
  126. }