Skip to content

Commit

Permalink
- Replaced UIWebView with WKWebview.
Browse files Browse the repository at this point in the history
- Updated demo project to support swift 5.1 (Xcode 11.2.1).
- Organized demo projects storyboards into one file.
- Cleaned up linting / formating issues.
- Fixed navigation issues with demo project.
  • Loading branch information
rlester committed Nov 11, 2019
1 parent 9c5c34a commit 8ff55fd
Show file tree
Hide file tree
Showing 28 changed files with 694 additions and 774 deletions.
2 changes: 1 addition & 1 deletion Demo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
Expand Down
93 changes: 0 additions & 93 deletions Demo/Base.lproj/Main.storyboard

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,21 @@ class PDFOutputViewController: UIViewController {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func render(_: UIButton) {
let dst = NSHomeDirectory() + "/test.pdf"
try! PDFGenerator.generate(self.scrollView, to: dst)
openPDFViewer(dst)
}

fileprivate func openPDFViewer(_ pdfPath: String) {
let url = URL(fileURLWithPath: pdfPath)
let storyboard = UIStoryboard(name: "PDFPreviewVC", bundle: nil)
let vc = storyboard.instantiateInitialViewController() as! PDFPreviewVC
vc.setupWithURL(url)
present(vc, animated: true, completion: nil)
self.performSegue(withIdentifier: "PreviewVC", sender: pdfPath)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let pdfPreviewVC = segue.destination as? PDFPreviewVC, let pdfPath = sender as? String {
let url = URL(fileURLWithPath: pdfPath)
pdfPreviewVC.setupWithURL(url)
}
}
*/

}
16 changes: 8 additions & 8 deletions Demo/PDFPreviewVC.swift → Demo/Controllers/PDFPreviewVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@
//

import UIKit
import WebKit

class PDFPreviewVC: UIViewController {
@IBOutlet fileprivate weak var webView: UIWebView!

@IBOutlet fileprivate weak var webView: WKWebView!
var url: URL!
override func viewDidLoad() {
super.viewDidLoad()
let req = NSMutableURLRequest(url: url)
req.timeoutInterval = 60.0
req.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData

webView.scalesPageToFit = true
webView.loadRequest(req as URLRequest)
// webView.scalesPageToFit = true
webView.load(req as URLRequest)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@objc @IBAction fileprivate func close(_ sender: AnyObject!) {

@IBAction fileprivate func close(_ sender: AnyObject!) {
dismiss(animated: true, completion: nil)
}

func setupWithURL(_ url: URL) {
self.url = url
}

}
59 changes: 59 additions & 0 deletions Demo/Controllers/SampleTableViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// SampleTableViewController.swift
// PDFGenerator
//
// Created by Suguru Kishimoto on 2016/02/27.
//
//

import UIKit
import PDFGenerator

class SampleTableViewController: UITableViewController {
@objc fileprivate func generatePDF() {
do {
let dst = NSHomeDirectory() + "/sample_tblview.pdf"
try PDFGenerator.generate(self.tableView, to: dst)
openPDFViewer(dst)
} catch let error {
print(error)
}

}

fileprivate func openPDFViewer(_ pdfPath: String) {
self.performSegue(withIdentifier: "PreviewVC", sender: pdfPath)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let pdfPreviewVC = segue.destination as? PDFPreviewVC, let pdfPath = sender as? String {
let url = URL(fileURLWithPath: pdfPath)
pdfPreviewVC.setupWithURL(url)
}
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
return 3
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! SampleTableViewCell
cell.leftLabel.text = "\((indexPath as NSIndexPath).section)-\((indexPath as NSIndexPath).row)cell"
cell.rightLabel.text = "sample"
return cell
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "section\(section)"
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
generatePDF()
}
}
64 changes: 18 additions & 46 deletions Demo/ViewController.swift → Demo/Controllers/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,26 @@ import UIKit
import PDFGenerator

class ViewController: UIViewController {

fileprivate var outputAsData: Bool = false

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
fileprivate var outputAsData: Bool = false

fileprivate func getImagePath(_ number: Int) -> String {
return Bundle.main.path(forResource: "sample_\(number)", ofType: "jpg")!
}

fileprivate func getDestinationPath(_ number: Int) -> String {
return NSHomeDirectory() + "/sample\(number).pdf"
}
@objc @IBAction fileprivate func generateSamplePDFFromViews(_ sender: AnyObject?) {

@IBAction fileprivate func generateSamplePDFFromViews(_ sender: AnyObject?) {
let v1 = UIScrollView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let v2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
let v3 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
v1.backgroundColor = UIColor.red
v1.contentSize = CGSize(width: 100, height: 100)
v2.backgroundColor = UIColor.green
v3.backgroundColor = UIColor.blue

do {
let dst = getDestinationPath(1)
if outputAsData {
Expand All @@ -53,8 +43,8 @@ class ViewController: UIViewController {
print(e)
}
}
@objc @IBAction fileprivate func generateSamplePDFFromImages(_ sender: AnyObject?) {

@IBAction fileprivate func generateSamplePDFFromImages(_ sender: AnyObject?) {
let dst = getDestinationPath(2)
autoreleasepool {
do {
Expand All @@ -74,8 +64,8 @@ class ViewController: UIViewController {
}
}
}
@objc @IBAction fileprivate func generateSamplePDFFromImagePaths(_ sender: AnyObject?) {

@IBAction fileprivate func generateSamplePDFFromImagePaths(_ sender: AnyObject?) {
do {
let dst = getDestinationPath(3)
var imagePaths = [String]()
Expand All @@ -93,13 +83,13 @@ class ViewController: UIViewController {
print(e)
}
}
@objc @IBAction fileprivate func generateSamplePDFFromPages(_ sender: AnyObject?) {

@IBAction fileprivate func generateSamplePDFFromPages(_ sender: AnyObject?) {
let v1 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
v1.backgroundColor = UIColor.red
let v2 = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 200))
v2.backgroundColor = UIColor.green

let page1 = PDFPage.view(v1)
let page2 = PDFPage.view(v2)
let page3 = PDFPage.whitePage(CGSize(width: 200, height: 100))
Expand All @@ -117,35 +107,17 @@ class ViewController: UIViewController {
openPDFViewer(dst)
} catch let e {
print(e)

}
}

@objc @IBAction fileprivate func generatePDFFromStackedScrollView(_: AnyObject?) {
let storyboard = UIStoryboard(name: "PDFOutput", bundle: nil)
let vc = storyboard.instantiateInitialViewController()!

present(vc, animated: true, completion: nil)
}

fileprivate func openPDFViewer(_ pdfPath: String) {
let url = URL(fileURLWithPath: pdfPath)
let storyboard = UIStoryboard(name: "PDFPreviewVC", bundle: nil)
let vc = storyboard.instantiateInitialViewController() as! PDFPreviewVC
vc.setupWithURL(url)
present(vc, animated: true, completion: nil)
self.performSegue(withIdentifier: "PreviewVC", sender: pdfPath)
}

@objc @IBAction fileprivate func goSampleTableView(_ sender: AnyObject?) {
let storyboard = UIStoryboard(name: "SampleTableViewController", bundle: nil)
let vc = storyboard.instantiateInitialViewController() as! SampleTableViewController
present(vc, animated: true, completion: nil)
}

@objc @IBAction fileprivate func goSampleWebView(_ sender: AnyObject?) {
let storyboard = UIStoryboard(name: "WebViewController", bundle: nil)
let vc = storyboard.instantiateInitialViewController() as! WebViewController
present(vc, animated: true, completion: nil)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let pdfPreviewVC = segue.destination as? PDFPreviewVC, let pdfPath = sender as? String {
let url = URL(fileURLWithPath: pdfPath)
pdfPreviewVC.setupWithURL(url)
}
}

}
Loading

0 comments on commit 8ff55fd

Please sign in to comment.