|
| 1 | +# Make Your iOS and Android App Secure |
| 2 | + |
| 3 | +## Block ScreenShots and Screen Recording without adding any packages in Flutter |
| 4 | + |
| 5 | +:bulb: Notic: Only works on real Device | the codes dosen't works on simulator for both platform |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +## Android |
| 10 | + |
| 11 | +Add this to `MainActivity` in android/app/src/main/kotlin/com/PackegeName/ProjectName |
| 12 | + |
| 13 | +```kotlin |
| 14 | +package com.example.test // replace your packageName |
| 15 | + |
| 16 | +import android.view.WindowManager.LayoutParams |
| 17 | +import io.flutter.embedding.android.FlutterActivity |
| 18 | +import io.flutter.embedding.engine.FlutterEngine |
| 19 | + |
| 20 | +class MainActivity: FlutterActivity() { |
| 21 | + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { |
| 22 | + window.addFlags(LayoutParams.FLAG_SECURE) |
| 23 | + super.configureFlutterEngine(flutterEngine) |
| 24 | + } |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +## iOS |
| 31 | + |
| 32 | +Add this to `AppDelegate` in ios/Runner/AppDelegate.swift |
| 33 | + |
| 34 | +```Swift |
| 35 | +import UIKit |
| 36 | +import Flutter |
| 37 | + |
| 38 | + |
| 39 | +@UIApplicationMain |
| 40 | +@objc class AppDelegate: FlutterAppDelegate { |
| 41 | + override func application( |
| 42 | + _ application: UIApplication, |
| 43 | + didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? |
| 44 | + ) -> Bool { |
| 45 | + self.window.makeSecure() // + add this line |
| 46 | + GeneratedPluginRegistrant.register(with: self) |
| 47 | + return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// + add this section |
| 52 | + |
| 53 | +extension UIWindow { |
| 54 | +func makeSecure() { |
| 55 | + let field = UITextField() |
| 56 | + field.isSecureTextEntry = true |
| 57 | + self.addSubview(field) |
| 58 | + field.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true |
| 59 | + field.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true |
| 60 | + self.layer.superlayer?.addSublayer(field.layer) |
| 61 | + field.layer.sublayers?.first?.addSublayer(self.layer) |
| 62 | + } |
| 63 | +} |
| 64 | +``` |
0 commit comments