提交 8c02fe28 编写于 作者: lizhongyi_'s avatar lizhongyi_

uni-usercapturescreen 插件iOS调整

上级 a2acc722
import { NotificationCenter } from 'Foundation'; import { NotificationCenter } from 'Foundation';
import { CGRect } from "CoreFoundation"; import { CGRect } from "CoreFoundation";
import { UIApplication, UIView, UITextField, UIScreen, UIDevice } from "UIKit" import { UIApplication, UIView, UITextField, UIScreen, UIDevice } from "UIKit"
import { UTSiOS, UTSCallback } from "DCloudUTSFoundation" import { UTSiOS } from "DCloudUTSFoundation"
import { DispatchQueue } from 'Dispatch'; import { DispatchQueue } from 'Dispatch';
import { SetUserCaptureScreenOption, UserCaptureScreenResult, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen } from "../interface.uts" import { SetUserCaptureScreenOptions, UserCaptureScreenResult, OnUserCaptureScreen, OffUserCaptureScreen, SetUserCaptureScreen, UserCaptureScreenCallback, OnUserCaptureScreenResult } from "../interface.uts"
/** /**
* 定义监听截屏事件工具类 * 定义监听截屏事件工具类
*/ */
class CaptureScreenTool { class CaptureScreenTool {
static listener ?: UTSCallback; static listener : UserCaptureScreenCallback | null;
static secureView ?: UIView; static secureView : UIView | null;
// 监听截屏 // 监听截屏
static listenCaptureScreen(callback ?: UTSCallback) { static listenCaptureScreen(callback : UserCaptureScreenCallback | null) {
this.listener = callback this.listener = callback
// 注册监听截屏事件及回调方法 // 注册监听截屏事件及回调方法
// target-action 回调方法需要通过 Selector("方法名") 构建 // target-action 回调方法需要通过 Selector("方法名") 构建
const method = Selector("userDidTakeScreenshot") const method = Selector("userDidTakeScreenshot")
...@@ -25,35 +25,45 @@ class CaptureScreenTool { ...@@ -25,35 +25,45 @@ class CaptureScreenTool {
// 捕获截屏回调的方法 // 捕获截屏回调的方法
// target-action 的方法前需要添加 @objc 前缀 // target-action 的方法前需要添加 @objc 前缀
@objc static userDidTakeScreenshot() { @objc static userDidTakeScreenshot() {
// 回调 // 回调
this.listener?.({}) const res: OnUserCaptureScreenResult = {
errCode: 0,
errSubject: "uni-usercapturescreen",
errMsg: "onUserCaptureScreen: ok",
path: null
}
this.listener?.(res)
} }
// 移除监听事件 // 移除监听事件
static removeListen(callback ?: UTSCallback) { static removeListen(callback : UserCaptureScreenCallback | null) {
this.listener = null this.listener = null
NotificationCenter.default.removeObserver(this) NotificationCenter.default.removeObserver(this)
callback?.({}) const res: OnUserCaptureScreenResult = {
errCode: 0,
errSubject: "uni-usercapturescreen",
errMsg: "offUserCaptureScreen: ok",
path: null
}
callback?.(res)
} }
static createSecureView() : UIView | null { static createSecureView() : UIView | null {
let field = new UITextField(frame = CGRect.zero) let field = new UITextField(frame = CGRect.zero)
field.isSecureTextEntry = true field.isSecureTextEntry = true
if (field.subviews.count > 0 && UIDevice.current.systemVersion != '15.1') { if (field.subviews.length > 0 && UIDevice.current.systemVersion != '15.1') {
let view = field.subviews.first let view = field.subviews[0]
if (view != null) { view.subviews.forEach((item) => {
view!.subviews.forEach((item) => { item.removeFromSuperview()
item.removeFromSuperview() })
}) view.isUserInteractionEnabled = true
view!.isUserInteractionEnabled = true return view
return view
}
} }
return null return null
} }
// 开启防截屏 // 开启防截屏
static onAntiScreenshot(option : SetUserCaptureScreenOption) { static onAntiScreenshot(option : SetUserCaptureScreenOptions) {
// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行 // uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
DispatchQueue.main.async(execute = () : void => { DispatchQueue.main.async(execute = () : void => {
let secureView = this.createSecureView() let secureView = this.createSecureView()
...@@ -70,18 +80,19 @@ class CaptureScreenTool { ...@@ -70,18 +80,19 @@ class CaptureScreenTool {
secureView!.frame = UIScreen.main.bounds secureView!.frame = UIScreen.main.bounds
rootView!.frame = rect rootView!.frame = rect
} }
}
let res: UserCaptureScreenResult = {
errCode: 0,
errSubject: "uni-usercapturescreen",
errMsg: "setUserCaptureScreen:ok"
} }
let res = new UserCaptureScreenResult()
res.errCode = 0;
res.errSubject = "uni-usercapturescreen";
res.errMsg = "setUserCaptureScreen:ok";
option.success?.(res) option.success?.(res)
option.complete?.(res) option.complete?.(res)
}) })
} }
// 关闭防截屏 // 关闭防截屏
static offAntiScreenshot(option : SetUserCaptureScreenOption) { static offAntiScreenshot(option : SetUserCaptureScreenOptions) {
DispatchQueue.main.async(execute = () : void => { DispatchQueue.main.async(execute = () : void => {
if (this.secureView != null) { if (this.secureView != null) {
let window = UTSiOS.getKeyWindow() let window = UTSiOS.getKeyWindow()
...@@ -94,11 +105,12 @@ class CaptureScreenTool { ...@@ -94,11 +105,12 @@ class CaptureScreenTool {
} }
} }
this.secureView = null this.secureView = null
}
let res: UserCaptureScreenResult = {
errCode: 0,
errSubject: "uni-usercapturescreen",
errMsg: "setUserCaptureScreen:ok"
} }
let res = new UserCaptureScreenResult()
res.errCode = 0;
res.errSubject = "uni-usercapturescreen";
res.errMsg = "setUserCaptureScreen:ok";
option.success?.(res) option.success?.(res)
option.complete?.(res) option.complete?.(res)
}) })
...@@ -108,34 +120,36 @@ class CaptureScreenTool { ...@@ -108,34 +120,36 @@ class CaptureScreenTool {
/** /**
* 开启截图监听 * 开启截图监听
*/ */
export const onUserCaptureScreen : OnUserCaptureScreen = function (callback ?: UTSCallback) { export const onUserCaptureScreen : OnUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
CaptureScreenTool.listenCaptureScreen(callback) CaptureScreenTool.listenCaptureScreen(callback)
} }
/** /**
* 关闭截屏监听 * 关闭截屏监听
*/ */
export const offUserCaptureScreen : OffUserCaptureScreen = function (callback ?: UTSCallback) { export const offUserCaptureScreen : OffUserCaptureScreen = function (callback : UserCaptureScreenCallback | null) {
CaptureScreenTool.removeListen(callback) CaptureScreenTool.removeListen(callback)
} }
/** /**
* 开启/关闭防截屏 * 开启/关闭防截屏
*/ */
export const setUserCaptureScreen : SetUserCaptureScreen = function (options : SetUserCaptureScreenOption) { export const setUserCaptureScreen : SetUserCaptureScreen = function (options : SetUserCaptureScreenOptions) {
if (UIDevice.current.systemVersion < "13.0") { if (UIDevice.current.systemVersion < "13.0") {
let res = new UserCaptureScreenResult() let res: UserCaptureScreenResult = {
res.errCode = 12001; errCode: 12001,
res.errSubject = "uni-usercapturescreen"; errSubject: "uni-usercapturescreen",
res.errMsg = "setUserCaptureScreen:system not support"; errMsg: "setUserCaptureScreen:system not support"
}
options.fail?.(res); options.fail?.(res);
options.complete?.(res); options.complete?.(res);
} else if (UIDevice.current.systemVersion == "15.1") { } else if (UIDevice.current.systemVersion == "15.1") {
let res = new UserCaptureScreenResult() let res: UserCaptureScreenResult = {
res.errCode = 12010; errCode: 12010,
res.errSubject = "uni-usercapturescreen"; errSubject: "uni-usercapturescreen",
res.errMsg = "setUserCaptureScreen:system internal error"; errMsg: "setUserCaptureScreen:system internal error"
}
options.fail?.(res); options.fail?.(res);
options.complete?.(res); options.complete?.(res);
} else { } else {
......
...@@ -2,6 +2,7 @@ export type UserCaptureScreenResult = { ...@@ -2,6 +2,7 @@ export type UserCaptureScreenResult = {
/** /**
* 错误码 * 错误码
* 0:成功 * 0:成功
* -1:permission denied
* 12001:system not support * 12001:system not support
* 12010:system internal error * 12010:system internal error
*/ */
...@@ -81,7 +82,7 @@ export type OffUserCaptureScreen = (callback : UserCaptureScreenCallback | null) ...@@ -81,7 +82,7 @@ export type OffUserCaptureScreen = (callback : UserCaptureScreenCallback | null)
/** /**
* 设置防截屏 * 设置防截屏
* *
* @param {SetUserCaptureScreenOption} options * @param {SetUserCaptureScreenOptions} options
* @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#setusercapturescreen * @tutorial https://uniapp.dcloud.net.cn/api/system/capture-screen.html#setusercapturescreen
* @platforms APP-IOS = ^9.0,APP-ANDROID = ^22 * @platforms APP-IOS = ^9.0,APP-ANDROID = ^22
* @since 3.7.3 * @since 3.7.3
...@@ -92,4 +93,5 @@ interface uni { ...@@ -92,4 +93,5 @@ interface uni {
onUserCaptureScreen : OnUserCaptureScreen, onUserCaptureScreen : OnUserCaptureScreen,
offUserCaptureScreen : OffUserCaptureScreen, offUserCaptureScreen : OffUserCaptureScreen,
setUserCaptureScreen : SetUserCaptureScreen setUserCaptureScreen : SetUserCaptureScreen
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册