Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions ios/Classes/SwiftFilePickerWritablePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@ enum FilePickerError: Error {
case invalidArguments(message: String)
}

fileprivate func getRootViewController() -> UIViewController {
var vc: UIViewController?
if #available(iOS 13, *) {
for scene in UIApplication.shared.connectedScenes {
guard let scene = scene as? UIWindowScene else { continue }
for window in scene.windows {
guard window.isKeyWindow else { continue }
vc = window.rootViewController
}
}
} else {
vc = UIApplication.shared.keyWindow?.rootViewController
}
guard let vc = vc else {
NSLog("PANIC - no view controller available.")
fatalError("No viewController available.")
}
return vc
}

public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {

private let _viewController: UIViewController
private let _channel: FlutterMethodChannel
private var _filePickerResult: FlutterResult?
private var _filePickerPath: String?
Expand All @@ -19,17 +38,12 @@ public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {
private var _eventQueue: [[String: String]] = []

public static func register(with registrar: FlutterPluginRegistrar) {
guard let vc = UIApplication.shared.delegate?.window??.rootViewController else {
NSLog("PANIC - unable to initialize plugin, no view controller available.")
fatalError("No viewController available.")
}
_ = SwiftFilePickerWritablePlugin(viewController: vc, registrar: registrar)
_ = SwiftFilePickerWritablePlugin(registrar: registrar)
}

public init(viewController: UIViewController, registrar: FlutterPluginRegistrar) {
public init(registrar: FlutterPluginRegistrar) {

let channel = FlutterMethodChannel(name: "design.codeux.file_picker_writable", binaryMessenger: registrar.messenger())
_viewController = viewController;
_channel = channel

super.init()
Expand Down Expand Up @@ -165,7 +179,7 @@ public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {
let ctrl = UIDocumentPickerViewController(documentTypes: [kUTTypeFolder as String], in: UIDocumentPickerMode.open)
ctrl.delegate = self
ctrl.modalPresentationStyle = .currentContext
_viewController.present(ctrl, animated: true, completion: nil)
getRootViewController().present(ctrl, animated: true, completion: nil)
}

func openFilePicker(result: @escaping FlutterResult) {
Expand All @@ -178,7 +192,7 @@ public class SwiftFilePickerWritablePlugin: NSObject, FlutterPlugin {
let ctrl = UIDocumentPickerViewController(documentTypes: [kUTTypeItem as String], in: UIDocumentPickerMode.open)
ctrl.delegate = self
ctrl.modalPresentationStyle = .currentContext
_viewController.present(ctrl, animated: true, completion: nil)
getRootViewController().present(ctrl, animated: true, completion: nil)
}

private func _copyToTempDirectory(url: URL) throws -> URL {
Expand Down