mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-16 02:49:48 +12:00
75 lines
2.6 KiB
Swift
75 lines
2.6 KiB
Swift
// From https://gist.github.com/aheze/dbc7f9b452e4f86f2d8fe278b3c5001f
|
|
// DocumentPicker.swift
|
|
|
|
import UIKit
|
|
import MobileCoreServices
|
|
import UniformTypeIdentifiers
|
|
|
|
protocol DocumentDelegate: AnyObject {
|
|
func didPickDocument(document: Document?)
|
|
}
|
|
|
|
class Document: UIDocument {
|
|
var data: Data?
|
|
override func contents(forType typeName: String) throws -> Any {
|
|
guard let data = data else { return Data() }
|
|
return try NSKeyedArchiver.archivedData(withRootObject:data,
|
|
requiringSecureCoding: true)
|
|
}
|
|
override func load(fromContents contents: Any, ofType typeName:
|
|
String?) throws {
|
|
guard let data = contents as? Data else { return }
|
|
self.data = data
|
|
}
|
|
}
|
|
|
|
open class DocumentPicker: NSObject {
|
|
private var pickerController: UIDocumentPickerViewController?
|
|
private weak var presentationController: UIViewController?
|
|
private weak var delegate: DocumentDelegate?
|
|
|
|
private var pickedDocument: Document?
|
|
|
|
init(presentationController: UIViewController, delegate: DocumentDelegate) {
|
|
super.init()
|
|
self.presentationController = presentationController
|
|
self.delegate = delegate
|
|
}
|
|
|
|
public func displayPicker() {
|
|
self.pickerController = UIDocumentPickerViewController(forOpeningContentTypes: [UTType.data])
|
|
self.pickerController!.delegate = self
|
|
self.presentationController?.present(self.pickerController!, animated: true)
|
|
}
|
|
}
|
|
|
|
extension DocumentPicker: UIDocumentPickerDelegate {
|
|
/// delegate method, when the user selects a file
|
|
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
|
|
guard let url = urls.first else {
|
|
return
|
|
}
|
|
documentFromURL(pickedURL: url)
|
|
delegate?.didPickDocument(document: pickedDocument)
|
|
}
|
|
|
|
/// delegate method, when the user cancels
|
|
public func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
|
|
delegate?.didPickDocument(document: nil)
|
|
}
|
|
|
|
private func documentFromURL(pickedURL: URL) {
|
|
/// start accessing the resource
|
|
let shouldStopAccessing = pickedURL.startAccessingSecurityScopedResource()
|
|
|
|
defer {
|
|
if shouldStopAccessing {
|
|
pickedURL.stopAccessingSecurityScopedResource()
|
|
}
|
|
}
|
|
NSFileCoordinator().coordinate(readingItemAt: pickedURL, error: NSErrorPointer.none) { (readURL) in
|
|
let document = Document(fileURL: readURL)
|
|
pickedDocument = document
|
|
}
|
|
}
|
|
}
|