Skip to content

Instantly share code, notes, and snippets.

@vinniedaboi
Created August 12, 2022 19:31
Show Gist options
  • Select an option

  • Save vinniedaboi/cd8ecd2c40dd4c84be5e314722379d7a to your computer and use it in GitHub Desktop.

Select an option

Save vinniedaboi/cd8ecd2c40dd4c84be5e314722379d7a to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// IGiveUpAtThisPoint
//
// Created by vincent ng soon zheng on 12/08/2022.
//
import UIKit
import SwiftUI
import PDFKit
import Foundation
class ViewController: UIViewController {
@IBOutlet weak var UIButton: UIButton!
@IBOutlet weak var PDFfield: UITextField!
override func viewDidLoad(){
super.viewDidLoad()
PDFfield.delegate = self
UIButton.setTitle("Load Story", for: .normal)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
PDFfield.resignFirstResponder()
}
@IBAction func loadstory(_ sender: Any) {
let url = URL(string: PDFfield.text!)!
let pdfDoc = PDFDocument(url:url)!
PDFKitView(showing: pdfDoc)
}
}
extension ViewController : UITextFieldDelegate{
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
}
struct PDFKitView: UIViewRepresentable {
let pdfDocument: PDFDocument
init(showing pdfDoc: PDFDocument) {
self.pdfDocument = pdfDoc
}
//you could also have inits that take a URL or Data
func makeUIView(context: Context) -> PDFView {
let pdfView = PDFView()
pdfView.document = pdfDocument
pdfView.autoScales = true
return pdfView
}
func updateUIView(_ pdfView: PDFView, context: Context) {
pdfView.document = pdfDocument
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment