I tried implementing the solution in the answer of this question and I couldn't.
I'm trying to trigger an in-app safari browser with a pre-set link from a PDF widget button.
This is my code sample:
import UIKit
import PDFKit
import SafariServices
... //Safari VC declaration with the desired link
let sfVC = SFSafariViewController(url: URL(string: "https://www.apple.com/")!)
func insertLinkButtonInto(_ page: PDFPage) {
let pageBounds = page.bounds(for: .cropBox)
let linkButtonBounds = CGRect(x: 90, y: pageBounds.size.height - 300, width: 106, height: 32)
let linkButton = PDFAnnotation(bounds: linkButtonBounds, forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)
linkButton.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.button.rawValue)
linkButton.widgetControlType = .pushButtonControl
linkButton.caption = "Click link"
page.addAnnotation(linkButton)
let linkButtonAction = PDFActionURL(url: URL(string: "https://www.apple.com/")!)
// linkButtonAction.action = How do I implement the code?
}
This is the action I'm trying to trigger:
present(sfVC, animated: true)
This is the function that I didn't know how to implement:
func pdfViewWillClick(onLink sender: PDFView, with url: URL) {
print(url)
}
This is the code in ViewDidload:
override func viewDidLoad() {
super.viewDidLoad()
if let documentURL = Bundle.main.url(forResource: "PDFFileName", withExtension: "pdf"),
let document = PDFDocument(url: documentURL),
let page = document.page(at: 0) {
// Set our document to the view, center it, and set a background color
pdfView?.document = document
pdfView?.autoScales = true
pdfView?.backgroundColor = UIColor.lightGray
self.insertLinkButtonInto(page)
}
I know the solution might be very easy for an experience programmer. I would really appreciate the help.
