forked from cabeca/RadarKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RadarKit.swift
98 lines (85 loc) · 3.29 KB
/
RadarKit.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
// Copyright 2018 Citrix Systems, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
import Foundation
import WebKit
public class RadarKit : NSObject, WKNavigationDelegate {
public func start(forZoneId: Int, customerId: Int) {
commands.append("cedexis.start(\(forZoneId),\(customerId),\(clientProfile),\(clientProfileVersion));");
process()
}
public func didReceiveMemoryWarning() {
unload()
}
private var webView: WKWebView?
private var radarLoaded = false
// 1 is the client profile for iOS (Swift)
private let clientProfile = 1
// The profile version expresses the version of the Radar runner code used
// to invoke the webview
private let clientProfileVersion = 1
private var commands: [String] = []
private var hibernateUntil = Date()
private func process() {
if (Date() < hibernateUntil) {
commands.removeAll()
return
}
if (webView == nil) {
webView = WKWebView ();
webView!.navigationDelegate = self
let url = URL (string: "https://radar.cedexis.com/0/0/radar.html");
let request = URLRequest(url: url!);
webView!.load(request);
}
if (radarLoaded) {
commands.forEach({ (command) in
webView!.evaluateJavaScript(command)
})
commands.removeAll()
}
}
private func unload() {
if (webView != nil) {
webView!.navigationDelegate = nil
webView = nil
radarLoaded = false
}
}
// When anything bad happens we shut down for one hour.
// Typically, this would happen when not connected to the internet.
private func hibernate() {
unload()
commands.removeAll()
hibernateUntil = Date() + 60 * 60;
}
// Called when the navigation is complete.
public func webView(_: WKWebView, didFinish: WKNavigation!) {
radarLoaded = true
process()
}
// Called when an error occurs during navigation.
public func webView(_: WKWebView, didFail: WKNavigation!, withError: Error) {
hibernate()
}
// Called when an error occurs while the web view is loading content.
public func webView(_: WKWebView, didFailProvisionalNavigation: WKNavigation!, withError: Error) {
hibernate()
}
// Called when the web view’s web content process is terminated.
public func webViewWebContentProcessDidTerminate(_: WKWebView) {
hibernate()
}
}