[swift] screenshot
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import UIKit
- import Flutter
- @UIApplicationMain
- @objc class AppDelegate: FlutterAppDelegate, FlutterStreamHandler {
- let overlayController = UIViewController()
- let textField = UITextField()
- var eventSink: FlutterEventSink?
- override func application(
- _ application: UIApplication,
- didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
- ) -> Bool {
- self.window.makeSecure()
- startTimer()
- if #available(iOS 10.0, *) {
- UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
- }
- makeSecureYourScreen()
- // Configure ScreenRecordHandler if it's defined
- // ScreenRecordHandler.shared.configure()
- GeneratedPluginRegistrant.register(with: self)
- NotificationCenter.default.addObserver(self, selector: #selector(screenRecordingStatusChanged), name: UIScreen.capturedDidChangeNotification, object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(handleScreenshot), name: UIApplication.userDidTakeScreenshotNotification, object: nil)
- DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
- if UIScreen.main.isCaptured {
- self.displayOverlayControllerWith(message: "Screen recording is not allowed while using the app. Kindly turn off the screen recording to continue using the app.")
- }
- }
- // Access FlutterViewController through the window property
- if let controller = window?.rootViewController as? FlutterViewController {
- let chargingChannel = FlutterEventChannel(name: "hurab/screentshot", binaryMessenger: controller.binaryMessenger)
- chargingChannel.setStreamHandler(self)
- } else {
- print("Error: FlutterViewController not found.")
- }
- return super.application(application, didFinishLaunchingWithOptions: launchOptions)
- }
- @objc func screenRecordingStatusChanged() {
- if UIScreen.main.isCaptured || isScreenshotBlocked() {
- self.displayOverlayControllerWith(message: "Screenshots are not allowed while using the app.")
- } else {
- self.overlayController.dismiss(animated: false, completion: nil)
- }
- }
- fileprivate func displayOverlayControllerWith(message: String) {
- if let rootWindow = UIApplication.shared.windows.first {
- self.overlayController.view.backgroundColor = .white
- self.overlayController.modalPresentationStyle = .fullScreen
- let screenWidth = UIScreen.main.bounds.width
- let screenHeight = UIScreen.main.bounds.height - (rootWindow.safeAreaInsets.top + rootWindow.safeAreaInsets.bottom)
- let frameOfLabel = CGRect(x: 20, y: screenHeight/2 - 100, width: screenWidth - 40, height: 200)
- if let labelMessage = self.overlayController.view.viewWithTag(1010) as? UILabel {
- labelMessage.text = message
- } else {
- let labelMessage = UILabel(frame: frameOfLabel)
- labelMessage.tag = 1010
- labelMessage.numberOfLines = 0
- labelMessage.font = UIFont.systemFont(ofSize: 18, weight: .regular)
- labelMessage.text = message
- labelMessage.textColor = .black
- labelMessage.textAlignment = .center
- self.overlayController.view.addSubview(labelMessage)
- }
- rootWindow.rootViewController?.present(self.overlayController, animated: false, completion: nil)
- }
- }
- override func applicationWillResignActive(
- _ application: UIApplication
- ) {
- print("applicationWillResignActive")
- self.window.isHidden = isRecording()
- }
- override func applicationDidBecomeActive(
- _ application: UIApplication
- ) {
- print("applicationDidBecomeActive")
- self.window.isHidden = isRecording()
- }
- @objc func isRecording() -> Bool {
- print("isRecording func")
- for screen in UIScreen.screens {
- if screen.isCaptured {
- screenshotInvoke()
- print("isRecording true")
- return true
- }
- }
- print("isRecording false")
- return false
- }
- private func screenshotInvoke() {
- guard let eventSink = eventSink else {
- return
- }
- eventSink("true")
- }
- override func applicationWillTerminate(_ application: UIApplication) {
- NotificationCenter.default.removeObserver(self)
- }
- private func makeSecureYourScreen() {
- if !self.window.subviews.contains(textField) {
- self.window.addSubview(textField)
- textField.centerYAnchor.constraint(equalTo: self.window.centerYAnchor).isActive = true
- textField.centerXAnchor.constraint(equalTo: self.window.centerXAnchor).isActive = true
- self.window.layer.superlayer?.addSublayer(textField.layer)
- textField.layer.sublayers?.first?.addSublayer(self.window.layer)
- }
- }
- func startTimer() {
- let timer = Timer.scheduledTimer(timeInterval: 10.0, target: self, selector: #selector(self.isRecording), userInfo: nil, repeats: true)
- }
- // MARK: - FlutterStreamHandler
- @objc func onListen(withArguments arguments: Any?, eventSink: @escaping FlutterEventSink) -> FlutterError? {
- // Implement your logic when a Flutter client starts listening
- // You can save the eventSink for later use
- self.eventSink = eventSink
- return nil
- }
- @objc func onCancel(withArguments arguments: Any?) -> FlutterError? {
- // Implement your logic when a Flutter client stops listening
- // You can clean up any resources here
- self.eventSink = nil
- return nil
- }
- // MARK: - Screenshot Handling
- @objc func handleScreenshot() {
- // Handle screenshot event
- print("Screenshot taken")
- // You can show an alert or perform any action here
- }
- // MARK: - Helper Method
- func isScreenshotBlocked() -> Bool {
- if #available(iOS 13.0, *) {
- if UIScreen.main.isCaptured || UIScreen.main.isCaptured {
- return true
- }
- } else {
- if UIScreen.main.isCaptured {
- return true
- }
- }
- return false
- }
- }
- extension UIWindow {
- func makeSecure() {
- let field = UITextField()
- field.isSecureTextEntry = true
- self.addSubview(field)
- field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
- field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
- self.layer.superlayer?.addSublayer(field.layer)
- field.layer.sublayers?.first?.addSublayer(self.layer)
- }
- }
Editor
You can edit this paste and save as new: