Trigger an In-App Safari Browser from a PDF Button Widget with PDFKIT - IOS

Viewed 91

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.

1 Answers

You are missing few things in your current code. In your viewDidLoad method set the delegate of the pdfView

override func viewDidLoad() {
    super.viewDidLoad()

    if let documentURL = Bundle.main.url(forResource: "Application", 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)
        //set the delegate of the pdfView to this viewController
        pdfView?.delegate = self
    }

Present the SFSafariViewController in delegate method with the url

func pdfViewWillClick(onLink sender: PDFView, with url: URL) {
    print(url)
    let sfVC = SFSafariViewController(url: url)
    self.present(sfVC, animated: true)
}

set the action for PDFAnnotation in

 func insertLinkButtonInto(_ page: PDFPage) {
        
        let pageBounds = page.bounds(for: .cropBox)
        
        let linkButtonBounds = CGRect(x: 90, y: 10, 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/")!)
        //set the linkButton action
        linkButton.action = linkButtonAction
    }

enter image description here

Related