Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//
// GoogleDocsViewController.swift
// Created by laiso on 2018/06/12.
//
// let gdViewController = GoogleDocsViewController(nibName: nil, bundle: nil)
// gdViewController.url = "https://docs.google.com/document/.../pub"
// let nav = UINavigationController(rootViewController: gdViewController)
// self.present(nav, animated: true, completion: nil)
//
import UIKit
import WebKit
class GoogleDocsViewController: UIViewController {
var url: String?
private let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
guard
let url = self.url,
let reqUrl = URL(string: url) else {
return
}
webView.navigationDelegate = self
webView.load(URLRequest(url: reqUrl))
self.view = webView
navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(close))
}
@objc func close() {
dismiss(animated: true) {
self.webView.stopLoading()
}
}
}
extension GoogleDocsViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.title") { result, err in
assert(err == nil, err?.localizedDescription ?? "")
self.title = result as? String
}
webView.evaluateJavaScript(
["document.getElementById('contents').style.margin='0 50px';",
"document.getElementById('header').hidden=true;",
"document.getElementById('footer').hidden=true;"].joined()) { result, err in
assert(err == nil, err?.localizedDescription ?? "")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment