-
Notifications
You must be signed in to change notification settings - Fork 24
/
SwiftNoScreenshotPlugin.swift
72 lines (56 loc) · 2.57 KB
/
SwiftNoScreenshotPlugin.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Flutter
import UIKit
import ScreenProtectorKit
public class SwiftNoScreenshotPlugin: NSObject, FlutterPlugin {
private var screenProtectorKit: ScreenProtectorKit? = nil
private static var channel: FlutterMethodChannel? = nil
static private var preventScreenShot: Bool = false
init(screenProtectorKit: ScreenProtectorKit) {
self.screenProtectorKit = screenProtectorKit
}
public static func register(with registrar: FlutterPluginRegistrar) {
SwiftNoScreenshotPlugin.channel = FlutterMethodChannel(name: "com.flutterplaza.no_screenshot", binaryMessenger: registrar.messenger())
let window = UIApplication.shared.delegate?.window
let screenProtectorKit = ScreenProtectorKit(window: window as? UIWindow)
screenProtectorKit.configurePreventionScreenshot()
let instance = SwiftNoScreenshotPlugin(screenProtectorKit: screenProtectorKit)
registrar.addMethodCallDelegate(instance, channel: SwiftNoScreenshotPlugin.channel!)
registrar.addApplicationDelegate(instance)
}
public func applicationWillResignActive(_ application: UIApplication) {
if SwiftNoScreenshotPlugin.preventScreenShot == true {
screenProtectorKit?.enabledPreventScreenshot()
} else if SwiftNoScreenshotPlugin.preventScreenShot == false {
screenProtectorKit?.disablePreventScreenshot()
}
}
public func applicationDidBecomeActive(_ application: UIApplication) {
if SwiftNoScreenshotPlugin.preventScreenShot == true {
screenProtectorKit?.enabledPreventScreenshot()
} else if SwiftNoScreenshotPlugin.preventScreenShot == false {
screenProtectorKit?.disablePreventScreenshot()
}
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if (call.method == "screenshotOff") {
SwiftNoScreenshotPlugin.preventScreenShot = false
shotOff()
} else if (call.method == "screenshotOn") {
SwiftNoScreenshotPlugin.preventScreenShot = true
shotOn()
} else if (call.method == "toggleScreenshot") {
SwiftNoScreenshotPlugin.preventScreenShot = !SwiftNoScreenshotPlugin.preventScreenShot;
SwiftNoScreenshotPlugin.preventScreenShot ? shotOn() : shotOff()
}
result(true)
}
private func shotOff() {
screenProtectorKit?.enabledPreventScreenshot()
}
private func shotOn() {
screenProtectorKit?.disablePreventScreenshot()
}
deinit {
screenProtectorKit?.removeAllObserver()
}
}