如何将 PDF 查看器实现到 SwiftUI 应用程序? [英] How to implement PDF Viewer to SwiftUI application?

查看:24
本文介绍了如何将 PDF 查看器实现到 SwiftUI 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个显示 PDF 的应用程序,但我不知道如何使用 SwiftUI 显示 PDF 文件.我找到了有关如何使用 UIKit 显示 PDF 文件的教程,但没有关于 SwiftUI 的教程.有人可以帮我吗?

I'm building an app, which displays PDFs, but I don't know how to display a PDF file using SwiftUI. I found tutorials on how to display a PDF file using UIKit, but there are no tutorials about SwiftUI. Can anyone help me?

我也在尝试使用 MVVM 设计模式来做到这一点.如果有人可以帮助我,我将不胜感激!

I'm also trying to do that using MVVM design pattern. If there's someone, who can help me, I will be extremely grateful!

import SwiftUI

struct HomeView: View {
    
    var deeds: [Deed] = deedsData
    
    var body: some View {
        NavigationView {
                List(deeds) { item in
                    Button(action: {
                        
                    }) {
                        HStack {
                            Image(systemName: "doc.fill")
                            Text(item.title)
                        }
                    }
                }
                .navigationTitle("title")
        }
    }
}

struct HomeView_Previews: PreviewProvider {
    static var previews: some View {
        HomeView(deeds: deedsData)
    }
}

DeedModel.swift

import SwiftUI

struct Deed: Identifiable {
    var id = UUID()
    var title: String
    var URL: String
}

let deedsData: [Deed] = [
    Deed(title: NSLocalizedString("civilCode", comment: "Civil code"), URL: "https://isap.sejm.gov.pl/isap.nsf/download.xsp/WDU19640160093/U/D19640093Lj.pdf"),
    Deed(title: NSLocalizedString("penalCode", comment: "Penal code"), URL: "https://isap.sejm.gov.pl/isap.nsf/download.xsp/WDU19970880553/U/D19970553Lj.pdf"),
    Deed(title: NSLocalizedString("civilProcedureCode", comment: "Code of civil procedure"), URL: "https://isap.sejm.gov.pl/isap.nsf/download.xsp/WDU19640430296/U/D19640296Lj.pdf"),
    Deed(title: NSLocalizedString("familyAndGuardianshipCode", comment: "Family and guardianship code"), URL: "http://isap.sejm.gov.pl/isap.nsf/download.xsp/WDU19640090059/U/D19640059Lj.pdf"),
    Deed(title: NSLocalizedString("laborCode", comment: "Labor code"), URL: "https://isap.sejm.gov.pl/isap.nsf/download.xsp/WDU19740240141/U/D19740141Lj.pdf"),
]

有人知道我如何在 MVVM 模式中做到这一点吗?

Anyone knows how can I do that in MVVM pattern?

推荐答案

要使用仅限 Apple 的框架显示 PDF,您需要通过 UIViewRepresentable 使用 UIKit:

To display a PDF with Apple-only frameworks, you'll need to use UIKit, via a UIViewRepresentable:

import PDFKit
import SwiftUI

struct PDFKitRepresentedView: UIViewRepresentable {
    typealias UIViewType = PDFView

    let data: Data
    let singlePage: Bool

    init(_ data: Data, singlePage: Bool = false) {
        self.data = data
        self.singlePage = singlePage
    }

    func makeUIView(context _: UIViewRepresentableContext<PDFKitRepresentedView>) -> UIViewType {
        // Create a `PDFView` and set its `PDFDocument`.
        let pdfView = PDFView()
        pdfView.document = PDFDocument(data: data)
        pdfView.autoScales = true
        if singlePage {
            pdfView.displayMode = .singlePage
        }
        return pdfView
    }

    func updateUIView(_ pdfView: UIViewType, context _: UIViewRepresentableContext<PDFKitRepresentedView>) {
        pdfView.document = PDFDocument(data: data)
    }
}

然后 PDFKitRepresentedView 可以在您的视图层次结构中使用.

then PDFKitRepresentedView can be used in your view hierarchy.

这需要一个 Data 对象作为输入,因此您需要通过网络调用将那些 URL 对象转换为 Data第一的.因此,您可能会执行以下操作:

This takes a Data object as input, so you'll need to convert those URL objects you have to Data via a network call first. So, you might do something like:

@State var data : Data? 

...

.onAppear {
  self.data = try? Data(contentsOf: url)
}

请记住,这大大简化了 - 您需要进行一些错误处理等.可能需要对 SwiftUI 网络调用进行一些搜索.

Keep in mind this is vastly simplified -- you'll want to do some error handling, etc. Might want to do some searching on SwiftUI network calls.

这篇关于如何将 PDF 查看器实现到 SwiftUI 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆