index.uts 4.4 KB
Newer Older
1
import { NotificationCenter } from 'Foundation';
lizhongyi_'s avatar
lizhongyi_ 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import { UIApplication, UIView, UITextField, UIScreen, UIDevice } from "UIKit"
import { UTSiOS } from "DCloudUTSFoundation"
import { DispatchQueue } from 'Dispatch';



/**
 * 设置防截屏options
 */
type SetUserCaptureScreenOption = {
	open: boolean;
	success?: (res: UTSJSONObject) => void;
	fail?: (res: UTSJSONObject) => void;
	complete?: (res: UTSJSONObject) => void;
}

18 19 20 21 22

/**
 * 定义监听截屏事件工具类
 */
class CaptureScreenTool {
lizhongyi_'s avatar
lizhongyi_ 已提交
23 24 25
	static listener?: UTSCallback;
	static secureView?: UIView;

26
	// 监听截屏
lizhongyi_'s avatar
lizhongyi_ 已提交
27
	static listenCaptureScreen(callback?: UTSCallback) {
28
		this.listener = callback
lizhongyi_'s avatar
lizhongyi_ 已提交
29 30

		// 注册监听截屏事件及回调方法
31 32 33 34
		// target-action 回调方法需要通过 Selector("方法名") 构建
		const method = Selector("userDidTakeScreenshot")
		NotificationCenter.default.addObserver(this, selector = method, name = UIApplication.userDidTakeScreenshotNotification, object = null)
	}
lizhongyi_'s avatar
lizhongyi_ 已提交
35

36 37 38
	// 捕获截屏回调的方法
	// target-action 的方法前需要添加 @objc 前缀
	@objc static userDidTakeScreenshot() {
lizhongyi_'s avatar
lizhongyi_ 已提交
39
		// 回调
40
		this.listener?.({})
41
	}
lizhongyi_'s avatar
lizhongyi_ 已提交
42

43
	// 移除监听事件
lizhongyi_'s avatar
lizhongyi_ 已提交
44
	static removeListen(callback?: UTSCallback) {
45 46
		this.listener = null
		NotificationCenter.default.removeObserver(this)
47
		callback?.({})
48
	}
lizhongyi_'s avatar
lizhongyi_ 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119

	static createSecureView(): UIView | null {
		let field = UITextField()
		field.isSecureTextEntry = true
		if (field.subviews.count > 0 && UIDevice.current.systemVersion != '15.1') {
			let view = field.subviews.first
			if (view != null) {
				view!.subviews.forEach((item) => {
					item.removeFromSuperview()
				})
				view!.isUserInteractionEnabled = true
				return view
			}
		}
		return null
	}

	// 开启防截屏
	static onAntiScreenshot(option: SetUserCaptureScreenOption) {
		// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
		DispatchQueue.main.async(execute = (): void => {
			let secureView = this.createSecureView()
			let window = UTSiOS.getKeyWindow()
			let rootView = window.rootViewController == null ? null : window.rootViewController!.view
			if (secureView != null && rootView != null) {
				let rootSuperview = rootView!.superview
				if (rootSuperview != null) {
					this.secureView = secureView
					rootSuperview!.addSubview(secureView!)
					rootView!.removeFromSuperview()
					secureView!.addSubview(rootView!)
					let rect = rootView!.frame
					secureView!.frame = UIScreen.main.bounds
					rootView!.frame = rect
				}
			}
			let res = {
				errSubject: "uts-screenshot-listener",
				errCode: 0,
				errMsg: "setUserCaptureScreen:ok",
			}
			option.success?.(res)
			option.complete?.(res)
		})
	}

	// 关闭防截屏
	static offAntiScreenshot(option: SetUserCaptureScreenOption) {
		DispatchQueue.main.async(execute = (): void => {
			if (this.secureView != null) {
				let window = UTSiOS.getKeyWindow()
				let rootView = window.rootViewController == null ? null : window.rootViewController!.view
				if (rootView != null && this.secureView!.superview != null) {
					let rootSuperview = this.secureView!.superview
					if (rootSuperview != null) {
						rootSuperview!.addSubview(rootView!)
						this.secureView!.removeFromSuperview()
					}
				}
				this.secureView = null
			}

			let res = {
				errSubject: "uts-screenshot-listener",
				errCode: 0,
				errMsg: "setUserCaptureScreen:ok",
			}
			option.success?.(res)
			option.complete?.(res)
		})
	}
120 121
}

lizhongyi_'s avatar
lizhongyi_ 已提交
122

123 124 125
/**
 * 开启截图监听
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
126
export function onUserCaptureScreen(callback?: UTSCallback) {
127 128 129 130 131 132
	CaptureScreenTool.listenCaptureScreen(callback)
}

/**
 * 关闭截屏监听
 */
lizhongyi_'s avatar
lizhongyi_ 已提交
133
export function offUserCaptureScreen(callback?: UTSCallback) {
134
	CaptureScreenTool.removeListen(callback)
lizhongyi_'s avatar
lizhongyi_ 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

/**
 * 开启/关闭防截屏
 */
export function setUserCaptureScreen(option: SetUserCaptureScreenOption) {
	if (UIDevice.current.systemVersion < "13.0") {
		let res = {
			errSubject: "uts-screenshot-listener",
			errCode: 12001,
			errMsg: "setUserCaptureScreen:system not support"
		}
		option.fail?.(res);
		option.complete?.(res);

	} else if (UIDevice.current.systemVersion == "15.1") {
		let res = {
			errSubject: "uts-screenshot-listener",
			errCode: 12010,
			errMsg: "setUserCaptureScreen:system internal error"
		}
		option.fail?.(res);
		option.complete?.(res);
	} else {
		if (option.open == true) {
			CaptureScreenTool.onAntiScreenshot(option)
		} else {
			CaptureScreenTool.offAntiScreenshot(option)
		}
	}
165
}