53 lines
2.2 KiB
Swift
53 lines
2.2 KiB
Swift
import Flutter
|
||||
|
|
import UIKit
|
|||
|
|
|
|||
|
|
@main
|
|||
|
|
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
|
|||
|
|
override func application(
|
|||
|
|
_ application: UIApplication,
|
|||
|
|
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
|
|||
|
|
) -> Bool {
|
|||
|
|
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
|
|||
|
|
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
|
|||
|
|
if let registrar = engineBridge.pluginRegistry.registrar(forPlugin: "SfzenithFiles") {
|
|||
|
|
let channel = FlutterMethodChannel(name: "sfzenith/files", binaryMessenger: registrar.messenger())
|
|||
|
|
channel.setMethodCallHandler { call, result in
|
|||
|
|
guard call.method == "saveToDownloads" else {
|
|||
|
|
result(FlutterMethodNotImplemented)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
let args = call.arguments as? [String: Any] ?? [:]
|
|||
|
|
let name = (args["name"] as? String) ?? "horizon.bin"
|
|||
|
|
let bytes = (args["bytes"] as? FlutterStandardTypedData)?.data ?? Data()
|
|||
|
|
do {
|
|||
|
|
result(try AppDelegate.saveToDocuments(name: name, data: bytes))
|
|||
|
|
} catch {
|
|||
|
|
result(FlutterError(code: "save_failed", message: error.localizedDescription, details: nil))
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// Saves into the app's Documents folder, which the Files app shows under
|
|||
|
|
/// On My iPhone › SkyContour (UIFileSharingEnabled). Never overwrites.
|
|||
|
|
static func saveToDocuments(name: String, data: Data) throws -> String {
|
|||
|
|
let fm = FileManager.default
|
|||
|
|
let dir = try fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
|||
|
|
let safe = (name as NSString).lastPathComponent
|
|||
|
|
let base = (safe as NSString).deletingPathExtension
|
|||
|
|
let ext = (safe as NSString).pathExtension
|
|||
|
|
var target = dir.appendingPathComponent(safe)
|
|||
|
|
var n = 1
|
|||
|
|
while fm.fileExists(atPath: target.path) {
|
|||
|
|
let candidate = ext.isEmpty ? "\(base) (\(n))" : "\(base) (\(n)).\(ext)"
|
|||
|
|
target = dir.appendingPathComponent(candidate)
|
|||
|
|
n += 1
|
|||
|
|
}
|
|||
|
|
try data.write(to: target, options: .atomic)
|
|||
|
|
return "Files › SkyContour › \(target.lastPathComponent)"
|
|||
|
|
}
|
|||
|
|
}
|