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