Skip to content

Instantly share code, notes, and snippets.

@InukVT
Created May 25, 2020 21:23
Show Gist options
  • Select an option

  • Save InukVT/ab64567ed54dde88f41b7da6aa82fed9 to your computer and use it in GitHub Desktop.

Select an option

Save InukVT/ab64567ed54dde88f41b7da6aa82fed9 to your computer and use it in GitHub Desktop.
import SwiftUI
struct DocumentPickerButton: View {
@State
var showPicker = false
var body: some View {
Button(action: {self.showPicker.toggle() }) {
Image(systemName: "plus.circle.fill")
.resizable()
}
.sheet(isPresented: self.$showPicker) {
DocumentPickerController()
.accentColor(.orange)
}
}
}
struct DocumentPickerController: UIViewControllerRepresentable {
typealias UIViewControllerType = DocumentPicker
let picker = UIViewControllerType(documentTypes: ["public.mp3"], in: .open)
let delagate = DocumentDelagate()
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
func makeUIViewController(context: Context) -> UIViewControllerType {
picker
}
}
class DocumentPicker: UIDocumentPickerViewController {
let documentDelegate = DocumentDelagate()
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self.documentDelegate
}
}
class DocumentDelagate: UIView ,UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController,
didPickDocumentsAt urls: [URL]) {
print("Reading URLS")
urls.forEach { url in
guard url.startAccessingSecurityScopedResource() else {
print("Failed to open the file")
return
}
print(url)
defer { url.stopAccessingSecurityScopedResource() }
guard let bookmark = try? url.bookmarkData() else {
return
}
let defaults = UserDefaults.standard
var array = defaults.array(forKey: "Songs") as? [Data]
array?.append(bookmark)
defaults.set(array, forKey: "Songs")
}
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Document picker was cancelled")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment