index.uts 4.7 KB

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