-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathAboutViewController.swift
96 lines (75 loc) · 3.11 KB
/
AboutViewController.swift
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
//
// InfoWKViewController.swift
// OpenGpxTracker
//
// Created by merlos on 24/09/14.
//
// Localized by nitricware on 19/08/19.
//
import UIKit
import WebKit
///
/// Controller to display the About page.
///
/// Internally it is a WKWebView that displays the resource file about.html.
///
class AboutViewController: UIViewController {
/// Embedded web browser
var webView: WKWebView?
/// Initializer. Only calls super
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
/// Initializer. Only calls super
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
///
/// Configures the view. Performs the following actions:
///
/// 1. Sets the title to About
/// 2. Adds "Done" button
/// 3. Adds the webview that loads about.html from the bundle.
///
override func viewDidLoad() {
super.viewDidLoad()
self.title = NSLocalizedString("ABOUT", comment: "no comment")
// Add the done button
let shareItem = UIBarButtonItem(title: NSLocalizedString("DONE", comment: "no comment"),
style: UIBarButtonItem.Style.plain, target: self,
action: #selector(AboutViewController.closeViewController))
self.navigationItem.rightBarButtonItems = [shareItem]
// Add the Webview
self.webView = WKWebView(frame: self.view.frame, configuration: WKWebViewConfiguration())
self.webView?.navigationDelegate = self
let path = Bundle.main.path(forResource: "about", ofType: "html")
let text = try? String(contentsOfFile: path!, encoding: String.Encoding.utf8)
webView?.loadHTMLString(text!, baseURL: nil)
webView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(webView!)
}
/// Closes this view controller. Triggered by pressing the "Done" button in navigation bar.
@objc func closeViewController() {
self.dismiss(animated: true, completion: { () -> Void in
})
}
}
/// Handles all navigation related stuff for the web view
extension AboutViewController: WKNavigationDelegate {
/// Opens Safari when user clicks a link in the About page.
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction,
decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
print("AboutViewController: decidePolicyForNavigationAction")
if navigationAction.navigationType == .linkActivated {
if #available(iOS 10, *) {
UIApplication.shared.open(navigationAction.request.url!)
} else {
UIApplication.shared.openURL(navigationAction.request.url!)
}
print("AboutViewController: external link sent to Safari")
decisionHandler(.cancel)
return
}
decisionHandler(.allow)
}
}