How to get file saved location after saving by option " Save to files"

Product: iOS SDK.

In my implementation, I have created an object of UIDocumentPickerViewController as documentPicker (for selecting the pdf from the File manager of iOS).
Then on Button (plusBtn) action I’m presenting the documentPicker view and on file selection it triggers the didPickDocumentsAt method defined in DashboardViewController extention. Here I’m calling the PTDocumentController’s function - openDocument with the file’s directory path. Then pushes the PTDocumentController.
After editing the file. When I tried to save the file by "save to file " option it gives the option for selecting the directory/ folder where I want to save the file(PDF) and option for naming the file. After the save I unable to get any file path or name of the file.

Please suggest some solution it will be great!

Here is the code that I have implemented to use the Pdf editor feature in my application.

import UIKit
import PDFNet
import Tools
import UniformTypeIdentifiers

//let appDelegate = UIApplication.shared.delegate as! AppDelegate

class DashboardViewController: BaseViewController {
    var selectedPDF: [URL] = []
    var sections: [String] = ["Recents"]
    var PDFFolder = URL.init(string: "")
    var PDFUrls: [URL] = []
    var tablecells: Int = 3
    private var closeBtn: UIBarButtonItem!

    let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["com.adobe.pdf"], in: UIDocumentPickerMode.import)

    let tabbedController = PTDocumentController()
    

//    documentController.navigationItem.rightBarButtonItems.append(myItem)
    
    @IBOutlet weak var toolsLbl: UILabel!
    @IBOutlet weak var searchImg: UIImageView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var homeLbl: UILabel!
    @IBOutlet weak var plusBtn: TEZShadowButton!
    
    @IBAction func homeBtn(_ sender: Any) {
    }
    @IBAction func plusBtn(_ sender: Any) {
        self.present(documentPicker, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        documentPicker.delegate = self
        documentPicker.allowsMultipleSelection = false
        documentPicker.modalPresentationStyle = .fullScreen
//        closeBtn = UIBarButtonItem(image: UIImage(systemName:"xmark"), style: .plain, target: nil, action: #selector(closesPDFeditor))
//        tabbedController.navigationItem.rightBarButtonItems?.append(closeBtn)
        tableView.delegate = self
        tableView.dataSource = self
        initUI()
        // Do any additional setup after loading the view.
    }
    
    
    override func viewDidAppear(_ animated: Bool)
    {
        PDFFolder = FM.createFolder(folderName: "PDF")
        plusBtn.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
        plusBtn.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        plusBtn.layer.shadowOpacity = 1.0
        plusBtn.layer.shadowRadius = 0.0
        plusBtn.layer.masksToBounds = false
        plusBtn.layer.cornerRadius = 4.0
        
        if let pdfUrls = FM.urls(for: PDFFolder!){
            tablecells = pdfUrls.count
            PDFUrls = pdfUrls
        }
    }
    
    override func viewWillAppear(_ animated: Bool)
    {
        if !appDelegate.isPro
        {
            appDelegate.loadMainView()
        }
        
        super.viewWillAppear(animated)
    }
    
    @objc func closesPDFeditor(){
        tabbedController.dismiss(animated: true)
    }
    
    private func initUI(){
        self.toolsLbl.text = "Files"
        self.searchImg.image = UIImage(systemName: "")
        self.homeLbl.text = "Home"
        self.homeLbl.textColor = UIColor(red: 0.808, green: 0.302, blue: 0.302, alpha: 1)
    }
    
    private func initializeData(){
        
    }
    
}

extension DashboardViewController: UITableViewDelegate, UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tablecells
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Cell1TableViewCell()
        return cell
    }
    
}

extension DashboardViewController:
                                        
                                        
    UIDocumentPickerDelegate,PTDocumentControllerDelegate, UIDocumentMenuDelegate, UINavigationControllerDelegate,PTDocumentViewControllerDelegate {
    func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: nil)
    }
    
    func documentViewController(_ documentViewController: PTDocumentViewController, destinationURLForDocumentAt sourceUrl: URL) -> URL? {
        return PDFFolder
    }
    
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        
        if controller.documentPickerMode == UIDocumentPickerMode.import {
            
            for fileUrl in urls
            {
                tabbedController.openDocument(with: fileUrl)
                self.navigationItem.backButtonTitle = ""
                self.navigationController?.pushViewController(tabbedController, animated: true)
            }
            
            controller.dismiss(animated: true)
        }
    }
    
    func documentController(_ documentController: PTDocumentController, didFailToOpenDocumentWithError error: Error) {
        documentController.dismiss(animated: true)
        print(error)
    }
    
    func documentController(_ documentController: PTDocumentController, destinationURLForDocumentAt sourceUrl: URL) -> URL? {
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[0]
    }
    
    override func export(_ sender: Any?) {
        print("Exported a file")
    }
    
}
```strong text

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Hi Abhi,

We do not have any control over where the document is saved using apple’s share sheet. Documents are auto-saved in the same location where they are opened. If you want to export the document to another location, you can look at saving it using the saveDocument method and copying it to the desired location.

If this does not work for you, could you create a ticket using this link and send a minimum working sample project? This would help us better understand the issue and resolve it efficiently.

Best Regards,
Sahil.

1 Like

Hi Sbehl,
Thanks for the quick response.

I have already looked in saveDocument, Under PTCoordinatedDocumentDelegate protocol I found this function - presentedItemDidMoveTo ( func coordinatedDocument(_ coordinatedDocument: PTCoordinatedDocument, presentedItemDidMoveTo newURL: URL ) ). I used this function but it is not called after saving the document the file with “Save to files” option in export.

Here I’m sharing my code:
(Find the function in the last part of the code.)

import UIKit
import PDFNet
import Tools
import UniformTypeIdentifiers

//let appDelegate = UIApplication.shared.delegate as! AppDelegate

class DashboardViewController: BaseViewController {
    var selectedPDF: [URL] = []
    var savedPDFs: [URL] = []
    var sections: [String] = ["Recents"]
    var PDFFolder = URL.init(string: "")
    var PDFUrls: [URL] = []
    var tablecells: Int = 3
    private var closeBtn: UIBarButtonItem!
    let documentPicker: UIDocumentPickerViewController = UIDocumentPickerViewController(documentTypes: ["com.adobe.pdf"], in: UIDocumentPickerMode.import)
    let tabbedController = PTDocumentController()
    

//    documentController.navigationItem.rightBarButtonItems.append(myItem)
    
    @IBOutlet weak var toolsLbl: UILabel!
    @IBOutlet weak var searchImg: UIImageView!
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet weak var homeLbl: UILabel!
    @IBOutlet weak var plusBtn: TEZShadowButton!
    
    @IBAction func homeBtn(_ sender: Any) {
    }
    @IBAction func plusBtn(_ sender: Any) {
        self.present(documentPicker, animated: true, completion: nil)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        documentPicker.delegate = self
        documentPicker.allowsMultipleSelection = false
        documentPicker.modalPresentationStyle = .fullScreen
//        closeBtn = UIBarButtonItem(image: UIImage(systemName:"xmark"), style: .plain, target: nil, action: #selector(closesPDFeditor))
//        tabbedController.navigationItem.rightBarButtonItems?.append(closeBtn)
        tableView.delegate = self
        tableView.dataSource = self
        initUI()
        // Do any additional setup after loading the view.
    }
    
    
    override func viewDidAppear(_ animated: Bool)
    {
        PDFFolder = FM.createFolder(folderName: "PDF")
        plusBtn.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.25).cgColor
        plusBtn.layer.shadowOffset = CGSize(width: 0.0, height: 2.0)
        plusBtn.layer.shadowOpacity = 1.0
        plusBtn.layer.shadowRadius = 0.0
        plusBtn.layer.masksToBounds = false
        plusBtn.layer.cornerRadius = 4.0
        
        if let pdfUrls = FM.urls(for: PDFFolder!){
            tablecells = pdfUrls.count
            PDFUrls = pdfUrls
        }
    }
    
    override func viewWillAppear(_ animated: Bool)
    {
        if !appDelegate.isPro
        {
            appDelegate.loadMainView()
        }
        
        super.viewWillAppear(animated)
    }
    
    @objc func closesPDFeditor(){
        tabbedController.dismiss(animated: true)
    }
    
    private func initUI(){
        self.toolsLbl.text = "Files"
        self.searchImg.image = UIImage(systemName: "")
        self.homeLbl.text = "Home"
        self.homeLbl.textColor = UIColor(red: 0.808, green: 0.302, blue: 0.302, alpha: 1)
    }
    
    private func initializeData(){
        
    }
    
}

extension DashboardViewController: UITableViewDelegate, UITableViewDataSource{
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        return sections[section]
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tablecells
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = Cell1TableViewCell()
        return cell
    }
    
}

extension DashboardViewController: UIDocumentPickerDelegate,PTDocumentControllerDelegate, UIDocumentMenuDelegate, UINavigationControllerDelegate,PTDocumentViewControllerDelegate {
    func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
        documentPicker.delegate = self
        present(documentPicker, animated: true, completion: nil)
    }
    
    func documentViewController(_ documentViewController: PTDocumentViewController, destinationURLForDocumentAt sourceUrl: URL) -> URL? {
        return PDFFolder
    }
    
    func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
    
    func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
        
        if controller.documentPickerMode == UIDocumentPickerMode.import {
            
            for fileUrl in urls
            {
                tabbedController.openDocument(with: fileUrl)
                self.navigationItem.backButtonTitle = ""
                self.navigationController?.pushViewController(tabbedController, animated: true)
            }
            
            controller.dismiss(animated: true)
        }
    }
    
    func documentController(_ documentController: PTDocumentController, didFailToOpenDocumentWithError error: Error) {
        documentController.dismiss(animated: true)
        print(error)
    }
    
    func documentController(_ documentController: PTDocumentController, destinationURLForDocumentAt sourceUrl: URL) -> URL? {
        let urls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        return urls[0]
    }
    
    override func export(_ sender: Any?) {
        print("Exported a file")
    }
    
}

extension DashboardViewController:  PTCoordinatedDocumentDelegate {
    
    func coordinatedDocumentDidChange(_ coordinatedDocument: PTCoordinatedDocument) {
        print("document modified.........")
    }
    
    func coordinatedDocumentShouldSave(_ coordinatedDocument: PTCoordinatedDocument) -> Bool {
        return true
    }
    
    func coordinatedDocumentShouldAutoSave(_ coordinatedDocument: PTCoordinatedDocument) -> Bool {
        return true
    }
    
    func coordinatedDocument(_ coordinatedDocument: PTCoordinatedDocument, presentedItemDidMoveTo newURL: URL) {
        savedPDFs.append(newURL)
        print(" saved to \(newURL)")
    }
    
}


Thanks, Abhishek.

For simplicity, this issue is being investigated in the ticket created using Freshdesk.

Best Regards,
Sahil Behl.