在阅读文档时保存用户进度。 Swift

Save user progress while reading a document. Swift

我有一个 table 的 pdf 文档。打开文档时,用户切换到具有自定义 class (PDFView).

的视图

需要保存最后一次阅读用户文档的地方,一键点击

查看读取文档的控制器代码。

import UIKit
import PDFKit

var nameFile:String?
var titleChapter:String?
class pdfView: UIViewController {


    //save progress doc
    @IBAction func SaveScrollAction(_ sender: UIBarButtonItem)
    {
        let  dest =   pdfDocView.currentDestination
    pdfDocView.go(to: dest!)

    }
    //Pdf view
    @IBOutlet var pdfDocView: PDFView!
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = titleChapter
        navigationController?.navigationBar.barTintColor = #colorLiteral(red: 0.5326591512, green: 0, blue: 0.002649205323, alpha: 0.8)

        //name Documents for view
        if let path = Bundle.main.path(forResource: nameFile,  ofType:"pdf") {
            if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
                pdfDocView.displayMode = .singlePageContinuous
                pdfDocView.autoScales = true
                pdfDocView.displayDirection = .vertical
                pdfDocView.document = pdfDocument
            }

        }

    }

}

我相信你可以使用属性 currentDestination,应该包含当前页面和位置。要在 PDFView 中设置目标对象,请使用方法 go(to destination: PDFDestination)

您可以使用常规存储解决方案保存对象。

Apple PDFKit Docs