@@ -30,15 +30,20 @@ public class SwiftFreeraspPlugin: NSObject, FlutterPlugin, FlutterStreamHandler
3030 /// - call: The `FlutterMethodCall` object representing the method call.
3131 /// - result: The `FlutterResult` object to be returned to the caller.
3232 public func handle( _ call: FlutterMethodCall , result: @escaping FlutterResult ) {
33- guard let args = call. arguments as? Dictionary < String , String >
34- else {
35- result ( FlutterError ( code: " talsec-failure " , message: " Unexpected arguments " , details: nil ) )
36- return
37- }
33+ let args = call. arguments as? Dictionary < String , Any > ?? [ : ]
3834
3935 switch call. method {
4036 case " start " :
41- start ( args: args, result: result)
37+ start ( configJson: args [ " config " ] as? String , result: result)
38+ return
39+ case " blockScreenCapture " :
40+ blockScreenCapture ( enable: args [ " enable " ] as? Bool , result: result)
41+ return
42+ case " isScreenCaptureBlocked " :
43+ isScreenCaptureBlocked ( result: result)
44+ return
45+ case " storeExternalId " :
46+ storeExternalId ( data: args [ " data " ] as? String , result: result)
4247 return
4348 default :
4449 result ( FlutterMethodNotImplemented)
@@ -50,9 +55,8 @@ public class SwiftFreeraspPlugin: NSObject, FlutterPlugin, FlutterStreamHandler
5055 /// - Parameters:
5156 /// - args: The arguments received from Flutter which contains configuration
5257 /// - result: The `FlutterResult` object to be returned to the caller.
53- private func start( args: Dictionary < String , String > , result: @escaping FlutterResult ) {
54- guard let json = args [ " config " ] ,
55- let data = json. data ( using: . utf8) ,
58+ private func start( configJson: String ? , result: @escaping FlutterResult ) {
59+ guard let data = configJson? . data ( using: . utf8) ,
5660 let flutterConfig = try ? JSONDecoder ( ) . decode ( FlutterTalsecConfig . self, from: data)
5761 else {
5862 result ( FlutterError ( code: " configuration-exception " , message: " Unable to decode configuration " , details: nil ) )
@@ -65,6 +69,69 @@ public class SwiftFreeraspPlugin: NSObject, FlutterPlugin, FlutterStreamHandler
6569 result ( nil )
6670 }
6771
72+ /// Blocks screen capture for the current UIWindow.
73+ ///
74+ /// - Parameters:
75+ /// - enable: Whether screen capture should be enabled / disabled.
76+ /// - result: The `FlutterResult` object to be returned to the caller.
77+ private func blockScreenCapture( enable: Bool ? , result: @escaping FlutterResult ) {
78+ guard let enableSafe = enable else {
79+ result ( FlutterError ( code: " block-screen-capture-failure " , message: " Couldn't process data. " , details: nil ) )
80+ return
81+ }
82+
83+ getProtectedWindow { window in
84+ if let window = window {
85+ Talsec . blockScreenCapture ( enable: enableSafe, window: window)
86+ result ( nil )
87+ } else {
88+ result ( FlutterError ( code: " block-screen-capture-failure " , message: " No windows found to block screen capture " , details: nil ) )
89+ }
90+ }
91+ }
92+
93+ /// Determines whether screen capture is blocked for the current UIWindow.
94+ ///
95+ /// - Parameters:
96+ /// - nonce: The nonce to be used in the cryptogram calculation.
97+ /// - result: The `FlutterResult` object to be returned to the caller.
98+ private func isScreenCaptureBlocked( result: @escaping FlutterResult ) {
99+ getProtectedWindow { window in
100+ if let window = window {
101+ let isBlocked = Talsec . isScreenCaptureBlocked ( in: window)
102+ result ( isBlocked)
103+ } else {
104+ result ( FlutterError ( code: " is-screen-capture-blocked-failure " , message: " Error while checking if screen capture is blocked " , details: nil ) )
105+ }
106+ }
107+ }
108+
109+ private func getProtectedWindow( completion: @escaping ( UIWindow ? ) -> Void ) {
110+ DispatchQueue . main. async {
111+ if #available( iOS 13 . 0 , * ) {
112+ if let windowScene = UIApplication . shared. connectedScenes. first as? UIWindowScene {
113+ if let window = windowScene. windows. first {
114+ completion ( window)
115+ } else {
116+ completion ( nil )
117+ }
118+ } else {
119+ completion ( nil )
120+ }
121+ }
122+ }
123+ }
124+
125+ /// Stores the external ID in user defaults.
126+ ///
127+ /// - Parameters:
128+ /// - data: The data to be stored.
129+ /// - result: The `FlutterResult` object to be returned to the caller.
130+ private func storeExternalId( data: String ? , result: @escaping FlutterResult ) {
131+ UserDefaults . standard. set ( data, forKey: " app.talsec.externalid " )
132+ result ( nil )
133+ }
134+
68135 /// Attaches a FlutterEventSink to the EventProcessor and processes any detectedThreats in the queue.
69136 ///
70137 /// - Parameters:
0 commit comments