Code: Select all
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool {
setupEventChannel()
if let options = launchOptions, let url = options[.url] as? URL, url.isFileURL {
pendingFilePath = handleFileUrl(url: url)
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions:launchOptions)
}
private func handleFileUrl(url: URL) -> String {
var filePath: String
let securityGranted = url.startAccessingSecurityScopedResource()
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileName = url.lastPathComponent
let destinationURL = documentsDirectory.appendingPathComponent(fileName)
do {
if FileManager.default.fileExists(atPath: destinationURL.path) {
try FileManager.default.removeItem(at: destinationURL)
}
try FileManager.default.copyItem(at: url, to: destinationURL)
filePath = destinationURL.path
} catch {
filePath = url.path
}
if securityGranted {
url.stopAccessingSecurityScopedResource()
}
return filePath
}