我正在尝试创建一个应用程序,其中包含一个公告列表,连接到Firebase服务器进行测试,如果Firebase具有pdf属性,我想在应用程序中显示它。
以下代码如下:
import UIKit
import FirebaseDatabaseUI
import Firebase
import Down
import FontAwesomeIconFactory
import PDFKit
extension AnnouncementDetailViewController: PDFViewDelegate {
func pdfViewWillClick(onLink sender: PDFView, with url: URL){
print(url)
}
}
class AnnouncementDetailViewController: UIViewController {
@IBOutlet var authorLabel: UILabel!
@IBOutlet var titleLabel: UILabel!
@IBOutlet var authorImage: UIImageView!
var announcementKey = ""
let announcement: Announcement = Announcement()
lazy var ref: DatabaseReference = Database.database().reference()
var announcementRef: DatabaseReference!
var refHandle: DatabaseHandle?
var contentView: DownView?
var pdfView: PDFView!
override func viewDidLoad() {
super.viewDidLoad()
initialiseContentView()
initialseDatabaseChild()
initialiseNavbarTitle()
//Method calls for Pdf
configureUI()
loadPDF()
playWithPDF()
}
private func configureUI(){
pdfView = PDFView ()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
pdfView.delegate = self
}
private func addObservers(){
NotificationCenter.default.addObserver(self, selector: #selector(handlePageChange(notification:)), name: Notification.Name.PDFViewPageChanged, object: nil)
}
@objc private func handlePageChange(notification: Notification){
print("Current page is changed")
}
private func loadPDF() {
guard
let url = URL(string:"http://www.pdf995.com/samples/pdf.pdf"),
let document = PDFDocument(url: url)
else { return }
if document.isLocked && document.unlock(withPassword: "test"){
pdfView.document = document
} else {
print("We have a problem")
}
}
private func playWithPDF(){
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
}
private func initialiseNavbarTitle() {
self.navigationItem.title = "Announcement Detail"
}
private func initialseDatabaseChild() {
announcementRef = ref.child("announcements").child(announcementKey)
}
private func generateDownViewHeight() -> CGFloat {
return UIScreen.main.bounds.height - authorLabel.frame.height - authorImage.frame.height - (navigationController?.navigationBar.frame.height ?? 0) - view.safeAreaInsets.top - 70
}
private func initialiseContentView() {
guard let contentView = try? DownView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), markdownString: "") else { return }
view.addSubview(contentView)
contentView.scrollView.bounces = false
contentView.translatesAutoresizingMaskIntoConstraints = false
contentView.topAnchor.constraint(equalTo: titleLabel.bottomAnchor, constant: 5).isActive = true
contentView.heightAnchor.constraint(equalToConstant: generateDownViewHeight()).isActive = true
contentView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width).isActive = true
self.contentView = contentView
}
@objc override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
refHandle = announcementRef.observe(DataEventType.value, with: { (snapshot) in
let announcementDict = snapshot.value as? [String : AnyObject] ?? [:]
self.announcement.setValuesForKeys(announcementDict)
do {
try self.contentView?.update(markdownString: self.announcement.content)
let factory = FontAwesomeIconFactory.button()
self.authorImage.image = factory.createImage(NIKFontAwesomeIcon.male)
self.authorLabel.text = self.announcement.author
self.titleLabel.text = self.announcement.title
}
catch {
}
})
}