Created
July 10, 2019 05:50
-
-
Save richo/61f308f5e4ba1fe7eefb8035aaa7817a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FilePickerPresentedView.swift | |
// flysight-grapher | |
// | |
// Created by richö butts on 7/8/19. | |
// Copyright © 2019 richö butts. All rights reserved. | |
// | |
import Foundation | |
import SwiftUI | |
import MobileCoreServices | |
struct FilePickerController: UIViewControllerRepresentable { | |
var callback: (URL) -> () | |
func makeCoordinator() -> Coordinator { | |
Coordinator(self) | |
} | |
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: UIViewControllerRepresentableContext<FilePickerController>) { | |
// Update the controller | |
} | |
func makeUIViewController(context: Context) -> UIDocumentPickerViewController { | |
print("Making the picker") | |
let controller = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText)], in: .open) | |
controller.delegate = context.coordinator | |
print("Setup the delegate \(context.coordinator)") | |
return controller | |
} | |
class Coordinator: NSObject, UIDocumentPickerDelegate { | |
var parent: FilePickerController | |
init(_ pickerController: FilePickerController) { | |
self.parent = pickerController | |
print("Setup a parent") | |
print("Callback: \(parent.callback)") | |
} | |
func documentPicker(didPickDocumentsAt: [URL]) { | |
print("Selected a document: \(didPickDocumentsAt[0])") | |
parent.callback(didPickDocumentsAt[0]) | |
} | |
func documentPickerWasCancelled() { | |
print("Document picker was thrown away :(") | |
} | |
deinit { | |
print("Coordinator going away") | |
} | |
} | |
} | |
struct PickerView: View { | |
var callback: (URL) -> () | |
var body: some View { | |
FilePickerController(callback: callback) | |
} | |
} | |
#if DEBUG | |
struct PickerView_Preview: PreviewProvider { | |
static var previews: some View { | |
func filePicked(_ url: URL) { | |
print("Filename: \(url)") | |
} | |
return PickerView(callback: filePicked) | |
.aspectRatio(3/2, contentMode: .fit) | |
} | |
} | |
#endif |
@OscarGorog thanks! Are these different for iOS and Catalyst? From memory it works in one place but not the other.
Hmmm... all I know is that the methods weren't being called so I checked the methods and they were different...
Gotcha, that also sounds like my experience. Thanks for posting the solution!
👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Correct methods:
documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
documentPickerWasCancelled(_ controller: UIDocumentPickerViewController)