-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebViewBaseViewController.swift
126 lines (87 loc) · 3.71 KB
/
WebViewBaseViewController.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//
// WebViewBaseViewController.swift
// Bourbon-iOS
//
// Parent class for VCs that use the webView in a similar fashion
//
// Created by Mitchell Kahn on 4/3/17.
// Copyright © 2017 Ourglass. All rights reserved.
//
import UIKit
import PKHUD
class WebViewBaseViewController: UIViewController, UIWebViewDelegate {
@IBOutlet weak var webView: UIWebView!
var targetUrlString: String?
override func viewDidLoad() {
super.viewDidLoad()
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
self.automaticallyAdjustsScrollViewInsets = false
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
webView.delegate = self
// webView.scrollView.isScrollEnabled = true
// webView.scrollView.bounces = true
webView.scrollView.isScrollEnabled = false
webView.scrollView.bounces = false
// prevent user from scaling or pinch zooming
webView.scalesPageToFit = false
webView.isMultipleTouchEnabled = false
// hide until loaded
webView.alpha = 0
// set title on navbar
// configure request timeout
let config = URLSessionConfiguration.default
config.timeoutIntervalForResource = TimeInterval(10)
goToTarget()
}
func goToTarget() {
guard let turl = targetUrlString, let url = URL(string: turl) else {
return
}
var urlReq = URLRequest(url: url,
cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
timeoutInterval: 10)
//headers: [ "Authorization" : "Bearer \(Settings.sharedInstance.userBelliniJWT ?? "")" ])
urlReq.setValue("Bearer \(Settings.sharedInstance.userBelliniJWT ?? "")", forHTTPHeaderField: "Authorization")
urlReq.setValue("OGHomey", forHTTPHeaderField: "X-OG-Mobile")
log.debug("url: \(url)")
HUD.show(.labeledProgress(title: "Loading", subtitle: "Please wait"))
self.webView.loadRequest(urlReq)
}
func webView(_ webView: UIWebView, didFailLoadWithError error: Error) {
log.debug("Error loading webview: \(error)")
let ecode = (error as NSError).code
if ecode == 102 {
log.debug("Stupid frame load error 102, ignoring");
return;
}
self.webViewDidTimeout()
}
func webViewDidTimeout() {
HUD.hide()
let alertController = UIAlertController(title: "Uh oh!", message: "There was a problem accessing the control view.", preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: {(alert:UIAlertAction!) in
_ = self.navigationController?.popViewController(animated: true)
}))
alertController.addAction(UIAlertAction(title: "Try again", style: .default, handler: {(alert:UIAlertAction!) in
self.goToTarget()
}))
self.present(alertController, animated: true, completion: nil)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
HUD.hide()
UIView.animate(withDuration: 1.0) {
self.webView.alpha = 1
}
}
// MARK: - Navigation
override func viewWillDisappear(_ animated: Bool) {
HUD.hide()
super.viewWillDisappear(animated)
}
// override func viewWillReapp(_ animated: Bool) {
// super.viewDidAppear(animated)
// self.webView.reload()
// }
}