I'm using UIDocumentPickerViewController to select a document from the iPhone.
I have implemented the feature in the following way.
class Si3EntityDocumentViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIDocumentInteractionControllerDelegate, UIDocumentPickerDelegate, UINavigationControllerDelegate, UIDocumentMenuDelegate {
override func viewDidLoad() {
super.viewDidLoad()
setLoader()
getDocuments(id:entity.id)
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.backgroundColor = .red
button.setTitle("Upload Doc", for: .normal)
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
self.view.addSubview(button)
// Do any additional setup after loading the view.
}
#objc func buttonAction(sender: UIButton!){
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypeText),String(kUTTypeContent),String(kUTTypeItem),String(kUTTypeData)], in: .import)
documentPicker.delegate = self
present(documentPicker, animated: true, completion: {
documentPicker.delegate = self
} )
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print(urls)
}
}
The func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt url: URL) method has been deprecated in iOS 11 so i have replaced it with func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
For some reason, I'm not getting the callback in func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL])
I also have some other code in this class but I've not included it.
When I select a document or click cancel I get the following error in the console
[DocumentManager] The view service did terminate with error: Error
Domain=_UIViewServiceErrorDomain Code=1 "(null)"
UserInfo={Terminated=disconnect method}
I know I'm missing out something very silly, any help is appreciated.
Thanks in anticipation.
You can have a look at the Apple documentation for a better understanding.
The default behaviour of a UIDocumentPickerViewController is to pick one document and you would have to use:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
}
If you are using the UIDocumentPickerViewController to pick multiple documents, then you will have to set the allowsMultipleSelection property to true and implement
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
}
Also, the following line would suffice.
present(documentPicker, animated: true)
I Used this Methods in swift 3
func openImportDocumentPicker() {
let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
documentPicker.delegate = self
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true, completion: { _ in })
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
if controller.documentPickerMode == .import {
let alertMessage: String = "Successfully imported \(url.absoluteURL)"
}
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Cancelled")
}
Try on a device. Simulator wasn't working for me when I logged into iCloud and tried to view a directory.
Related
I am new into this Xcode iOS stuff, I am trying to build a file uploader, you pick a file and then you upload to server. Currently I am stuck at picking the file. I have used some code found on this website, but I am getting some errors.
Class starts like this:
class DocumentsController: UIViewController, UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Methods used are:
#objc func pickFile() {
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePNG), String(kUTTypeJPEG)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let myURL = urls.first else {
return
}
print("import result : \(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
In the documentPicker method I have two errors:
'URL' is ambiguous for type lookup in this context <- for urls: [URL]
Type of expression is ambiguous without more context <- for guard let myURL = urls.first
If I replace URL with String or NSURL it gets past that, the build is successful, the app on the simulator is running (Iphone 8 plus), I can open the picker, choose a file, but then nothing happens. It just closes the picker and that's it. documentPicker method doesn't trigger. Out of the 4 methods only pickFile and documentMenu are triggering.
Xcode version is 13.3.1
Delegate method are not working for UIDocumentPickerViewController
from upload action want to load document from simulator for uploading to other action...
func uploadFile(){
let documentPickerController = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypeImage), String(kUTTypeMovie), String(kUTTypeVideo), String(kUTTypePlainText), String(kUTTypeMP3)], in: .import)
documentPickerController.delegate = self
present(documentPickerController, animated: true, completion: nil)
}
On selected didPickDocumentPicker noting happening in swift 5.0
extension UIViewController: UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
{
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let myURL = urls.first else {
return
}
print("import result : \(myURL)")
let fileName = urls.first?.lastPathComponent
print(fileName)
// self.loadUploadView(selectedFile: fileName!)
}
func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
}
For Device its working fine but for simulator its not working
else for iOS versions 13 its not working.
Device Version is 14
Simulator versions is 13.
I need to import to my app some document that have been saved into the "On My iPhone" folder. What would be the url of this folder? (those document (pdf) are saved outside the app by the user and I would like if possible to move/copy them to the app document folder but again I can't find that url.
Try this code (the way to pick needed document from iPhone Folder:
extension YourViewController: UIDocumentInteractionControllerDelegate {
/// If presenting a top a navigation stack, provide the navigation controller in order to animate in a manner consistent with the rest of the platform
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self.navigationController ?? self
}
}
extension YourViewController : UIDocumentPickerDelegate {
func initDocs() {
let pickerController = UIDocumentPickerViewController(documentTypes: ["public.item"], in: .import)
pickerController.delegate = self
if #available(iOS 11.0, *) {
pickerController.allowsMultipleSelection = false
}
present(pickerController, animated: false, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
if let url = urls.first {
self.handleDoc(url: url) // method in your view controller
}
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print("url = \(url)")
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}
}
In YourViewController call function:
#IBAction func importPem(_ sender: UIButton) {
initDocs()
UIDocumentInteractionController().presentPreview(animated: true)
}
I have integrated UIDocumentPickerDelegate or UIDocumentMenuDelegate, I have an issue to get the file from URL or DocumentPicker. How can I solve this issue in iOS swift?
You can get file easily like,
If you need to get image
#IBAction func btn_Handler_iCloud(_ sender: Any) {
let doc = UIDocumentMenuViewController(documentTypes: [String(kUTTypeImage)], in: .import)
doc.delegate = self
doc.modalPresentationStyle = .formSheet
self.present(doc, animated: true, completion: nil)
}
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let data = try! Data(contentsOf: urls[0])
imageview_Buaty.image = UIImage.init(data: data)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
dismiss(animated: true, completion: nil)
}
Getting document URL or data from UIDocumentPickerViewController:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
// Available from iOS 8.0 to iOS 11.0
self.handleFileSelection(inUrl: url)// Here url is document's URL
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
// Available from iOS 11.0
self.handleFileSelection(inUrl: urls.first!) // Here urls is array of URLs
}
private func handleFileSelection(inUrl:URL) -> Void {
do {
// inUrl is the document's URL
let data = try Data(contentsOf: inUrl) // Getting file data here
} catch {
dLog(message: "document loading error")
}
}
I have implemented UIDocumentPickerViewController in my class.implemented delegate methods.
I want to know how would I know which option from documentPickerMenu i have selected. I want to implement the dropbox selection in the presented menu of document controller. I want to know it from delegate method.
the methods are as below
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let cico = url as URL
print("The Url is : \(cico)")
}
func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
print(documentMenu.accessibilityLabel ?? "Menu")
present(documentPicker, animated: true, completion: nil)
}
func documentMenuWasCancelled(_ documentMenu: UIDocumentMenuViewController) {
print("we cancelled")
dismiss(animated: true, completion: nil)
}
if anyone can help me thanks in advance.