Matthias Nott
2026-04-01 1f5e5967edb4146b10f077dd7d38e73385b2ebfd
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
import Flutter
import UIKit
class SceneDelegate: FlutterSceneDelegate {
  override func scene(
    _ scene: UIScene,
    willConnectTo session: UISceneSession,
    options connectionOptions: UIScene.ConnectionOptions
  ) {
    super.scene(scene, willConnectTo: session, options: connectionOptions)
    guard let windowScene = scene as? UIWindowScene,
          let window = windowScene.windows.first,
          let flutterVC = window.rootViewController as? FlutterViewController
    else { return }
    setupBackupChannel(messenger: flutterVC.binaryMessenger)
  }
  /// Registers the com.mnsoft.pailot/backup MethodChannel so Dart can call
  /// NSURLIsExcludedFromBackupKey on the messages storage directory.
  private func setupBackupChannel(messenger: FlutterBinaryMessenger) {
    let channel = FlutterMethodChannel(
      name: "com.mnsoft.pailot/backup",
      binaryMessenger: messenger
    )
    channel.setMethodCallHandler { (call, result) in
      guard call.method == "excludeFromBackup" else {
        result(FlutterMethodNotImplemented)
        return
      }
      guard let path = call.arguments as? String else {
        result(FlutterError(code: "INVALID_ARG", message: "path argument required", details: nil))
        return
      }
      var url = URL(fileURLWithPath: path)
      var values = URLResourceValues()
      values.isExcludedFromBackup = true
      do {
        try url.setResourceValues(values)
        result(nil)
      } catch {
        result(FlutterError(code: "SET_FAILED", message: error.localizedDescription, details: nil))
      }
    }
  }
}