WebviewPreviewViewContoller.swift
4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// WebviewPreviewViewContoller.swift
// FileBrowser
//
// Created by Roy Marmelstein on 16/02/2016.
// Copyright © 2016 Roy Marmelstein. All rights reserved.
//
import UIKit
import WebKit
/// Webview for rendering items QuickLook will struggle with.
class WebviewPreviewViewContoller: UIViewController {
var webView = WKWebView()
var file: FBFile? {
didSet {
self.title = file?.displayName
self.processForDisplay()
}
}
//MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(webView)
// Add share button
let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(WebviewPreviewViewContoller.shareFile(_:)))
self.navigationItem.rightBarButtonItem = shareButton
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
webView.frame = self.view.bounds
}
//MARK: Share
@objc func shareFile(_ sender: UIBarButtonItem) {
guard let file = file else {
return
}
let activityViewController = UIActivityViewController(activityItems: [file.filePath], applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad &&
activityViewController.responds(to: #selector(getter: popoverPresentationController)) {
activityViewController.popoverPresentationController?.barButtonItem = sender
}
self.present(activityViewController, animated: true, completion: nil)
}
//MARK: Processing
func processForDisplay() {
guard let file = file, let data = try? Data(contentsOf: file.filePath as URL) else {
return
}
var rawString: String?
// Prepare plist for display
if file.type == .PLIST {
do {
if let plistDescription = try (PropertyListSerialization.propertyList(from: data, options: [], format: nil) as AnyObject).description {
rawString = plistDescription
}
} catch {}
}
// Prepare json file for display
else if file.type == .JSON {
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [])
if JSONSerialization.isValidJSONObject(jsonObject) {
let prettyJSON = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
var jsonString = String(data: prettyJSON, encoding: String.Encoding.utf8)
// Unescape forward slashes
jsonString = jsonString?.replacingOccurrences(of: "\\/", with: "/")
rawString = jsonString
}
} catch {}
}
// Default prepare for display
if rawString == nil {
rawString = String(data: data, encoding: String.Encoding.utf8)
}
// Convert and display string
if let convertedString = convertSpecialCharacters(rawString) {
let htmlString = "<html><head><meta name='viewport' content='initial-scale=1.0, user-scalable=no'></head><body><pre>\(convertedString)</pre></body></html>"
webView.loadHTMLString(htmlString, baseURL: nil)
}
}
// Make sure we convert HTML special characters
// Code from https://gist.github.com/mikesteele/70ae98d04fdc35cb1d5f
func convertSpecialCharacters(_ string: String?) -> String? {
guard let string = string else {
return nil
}
var newString = string
let char_dictionary = [
"&": "&",
"<": "<",
">": ">",
""": "\"",
"'": "'"
];
for (escaped_char, unescaped_char) in char_dictionary {
newString = newString.replacingOccurrences(of: escaped_char, with: unescaped_char, options: NSString.CompareOptions.regularExpression, range: nil)
}
return newString
}
}