| .. | .. |
|---|
| 3 | 3 | |
|---|
| 4 | 4 | class SceneDelegate: FlutterSceneDelegate { |
|---|
| 5 | 5 | |
|---|
| 6 | + override func scene( |
|---|
| 7 | + _ scene: UIScene, |
|---|
| 8 | + willConnectTo session: UISceneSession, |
|---|
| 9 | + options connectionOptions: UIScene.ConnectionOptions |
|---|
| 10 | + ) { |
|---|
| 11 | + super.scene(scene, willConnectTo: session, options: connectionOptions) |
|---|
| 12 | + guard let windowScene = scene as? UIWindowScene, |
|---|
| 13 | + let window = windowScene.windows.first, |
|---|
| 14 | + let flutterVC = window.rootViewController as? FlutterViewController |
|---|
| 15 | + else { return } |
|---|
| 16 | + setupBackupChannel(messenger: flutterVC.binaryMessenger) |
|---|
| 17 | + } |
|---|
| 18 | + |
|---|
| 19 | + /// Registers the com.mnsoft.pailot/backup MethodChannel so Dart can call |
|---|
| 20 | + /// NSURLIsExcludedFromBackupKey on the messages storage directory. |
|---|
| 21 | + private func setupBackupChannel(messenger: FlutterBinaryMessenger) { |
|---|
| 22 | + let channel = FlutterMethodChannel( |
|---|
| 23 | + name: "com.mnsoft.pailot/backup", |
|---|
| 24 | + binaryMessenger: messenger |
|---|
| 25 | + ) |
|---|
| 26 | + channel.setMethodCallHandler { (call, result) in |
|---|
| 27 | + guard call.method == "excludeFromBackup" else { |
|---|
| 28 | + result(FlutterMethodNotImplemented) |
|---|
| 29 | + return |
|---|
| 30 | + } |
|---|
| 31 | + guard let path = call.arguments as? String else { |
|---|
| 32 | + result(FlutterError(code: "INVALID_ARG", message: "path argument required", details: nil)) |
|---|
| 33 | + return |
|---|
| 34 | + } |
|---|
| 35 | + var url = URL(fileURLWithPath: path) |
|---|
| 36 | + var values = URLResourceValues() |
|---|
| 37 | + values.isExcludedFromBackup = true |
|---|
| 38 | + do { |
|---|
| 39 | + try url.setResourceValues(values) |
|---|
| 40 | + result(nil) |
|---|
| 41 | + } catch { |
|---|
| 42 | + result(FlutterError(code: "SET_FAILED", message: error.localizedDescription, details: nil)) |
|---|
| 43 | + } |
|---|
| 44 | + } |
|---|
| 45 | + } |
|---|
| 6 | 46 | } |
|---|