Не удается открыть загруженный путь PDF в iOS swift? - PullRequest
0 голосов
/ 07 мая 2018

Я пытаюсь реализовать опцию загрузки pdf, как только пользователь нажмет на опцию загрузки, pdf файл должен быть загружен и открыт. Я попробовал следующую ссылку, но это не работает Сохранение PDF-файлов с помощью Swift в iOS и отображение их , и я попробовал следующий код. В ответ на его показ Path я прошел этот путь, но не нашел ни одного файла.

 @objc func downloadPdfFile(_ sender : UIButton)
    {
        var localDic :NSDictionary!
        localDic = totalSyllabusArray.object(at: sender.tag) as! NSDictionary
        let filepath = localDic["filepath"] as! String
        let pdfURLString:String = FETCH_InMegh_Image_BaseURL + filepath
        let documentsUrl:URL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
        let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.pdf")
        //Create URL to the source file you want to download
        let fileURL = URL(string: pdfURLString)
        let sessionConfig = URLSessionConfiguration.default
        let session = URLSession(configuration: sessionConfig)
        let request = URLRequest(url:fileURL!)
        let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
            if let tempLocalUrl = tempLocalUrl, error == nil {
                // Success
                if let statusCode = (response as? HTTPURLResponse)?.statusCode {
                    print("Successfully downloaded. Status code: \(statusCode)")
                }
                do {
                    try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
                } catch (let writeError) {
                    print("Error creating a file \(destinationFileUrl) : \(writeError)")
                }
            } else {
                print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
            }
        }
        task.resume()
    }

1 Ответ

0 голосов
/ 09 мая 2018

Привет @kishore Apple недавно добавила PDfKit Framework, попробуйте эту ссылку ниже.

https://developer.apple.com/documentation/pdfkit

Здесь все, что вам нужно сделать, это взять One UIView в раскадровке для отображения файлов PDF. а в Identity Inspector установите класс как PDFView. Возьми розетку вот так.

@IBOutlet weak var pdfVw: PDFView!

И используйте следующий код.

 let url = URL(fileURLWithPath: "Pass your Pdf Path here")
            if let pdfDocument = PDFDocument(url: url) {
                pdfVw.autoScales = true
                pdfVw.displayMode = .singlePageContinuous
                pdfVw.displayDirection = .vertical
                pdfVw.document = pdfDocument
...