Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Console: iOS store cookies based on domain (#1198)

Michael Rademakerf09d5d1ed86f
diff --git a/console/iOS/ORLib/ORLib/UI/ORViewController.swift b/console/iOS/ORLib/ORLib/UI/ORViewController.swift
index b705cbb6c..4f92c360e 100644
--- a/console/iOS/ORLib/ORLib/UI/ORViewController.swift
+++ b/console/iOS/ORLib/ORLib/UI/ORViewController.swift
@@ -94,7 +94,7 @@ open class ORViewcontroller : UIViewController {
} else {
sbHeight = UIApplication.shared.statusBarFrame.height
}
- webCfg.websiteDataStore = WKWebsiteDataStore.default()
+
webCfg.allowsInlineMediaPlayback = true
let webFrame = CGRect(x: 0, y: sbHeight, width: view.frame.size.width, height: view.frame.size.height - sbHeight)
myWebView = WKWebView(frame: webFrame, configuration: webCfg)
@@ -372,7 +372,13 @@ extension ORViewcontroller: WKNavigationDelegate {
decisionHandler(.cancel)
}
} else {
- decisionHandler(.allow)
+ if let baseUrl = self.baseUrl {
+ webView.loadCookiesFromStorage(for: baseUrl) {
+ decisionHandler(.allow)
+ }
+ } else {
+ decisionHandler(.allow)
+ }
}
}
}
@@ -386,7 +392,13 @@ extension ORViewcontroller: WKNavigationDelegate {
return
}
}
- decisionHandler(.allow)
+ if let baseUrl = self.baseUrl {
+ webView.writeCookiesToStorage(for: baseUrl){
+ decisionHandler(.allow)
+ }
+ } else {
+ decisionHandler(.allow)
+ }
}
public func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
diff --git a/console/iOS/ORLib/ORLib/Utils/UIViewController+WKDelegate.swift b/console/iOS/ORLib/ORLib/Utils/UIViewController+WKDelegate.swift
new file mode 100644
index 000000000..8b1378917
--- /dev/null
+++ b/console/iOS/ORLib/ORLib/Utils/UIViewController+WKDelegate.swift
@@ -0,0 +1 @@
+
diff --git a/console/iOS/ORLib/ORLib/Utils/WKWebView+Utils.swift b/console/iOS/ORLib/ORLib/Utils/WKWebView+Utils.swift
new file mode 100644
index 000000000..a768c6520
--- /dev/null
+++ b/console/iOS/ORLib/ORLib/Utils/WKWebView+Utils.swift
@@ -0,0 +1,63 @@
+import WebKit
+
+extension WKWebView {
+
+enum PrefKey {
+ static let cookie = "cookies"
+}
+
+func writeCookiesToStorage(for domain: String, completion: @escaping () -> ()) {
+ fetchInMemoryCookies(for: domain) { data in
+ UserDefaults.standard.setValue(data, forKey: PrefKey.cookie + domain)
+ completion();
+ }
+}
+
+
+ func loadCookiesFromStorage(for domain: String, completion: @escaping () -> ()) {
+ if let storedCookie = UserDefaults.standard.dictionary(forKey: (PrefKey.cookie + domain)){
+ fetchInMemoryCookies(for: domain) { freshCookie in
+
+ let mergedCookie = storedCookie.merging(freshCookie) { (_, new) in new }
+
+ for (_, cookieConfig) in mergedCookie {
+ let cookie = cookieConfig as! Dictionary<String, Any>
+
+ var expire : Any? = nil
+
+ if let expireTime = cookie["Expires"] as? Double{
+ expire = Date(timeIntervalSinceNow: expireTime)
+ }
+
+ let newCookie = HTTPCookie(properties: [
+ .domain: cookie["Domain"] as Any,
+ .path: cookie["Path"] as Any,
+ .name: cookie["Name"] as Any,
+ .value: cookie["Value"] as Any,
+ .secure: cookie["Secure"] as Any,
+ .expires: expire as Any
+ ])
+
+ self.configuration.websiteDataStore.httpCookieStore.setCookie(newCookie!)
+ }
+
+ completion()
+ }
+
+ }
+ else{
+ completion()
+ }
+}
+
+func fetchInMemoryCookies(for domain: String, completion: @escaping ([String: Any]) -> ()) {
+ var cookieDict = [String: AnyObject]()
+ WKWebsiteDataStore.default().httpCookieStore.getAllCookies { (cookies) in
+ for cookie in cookies {
+ if domain.contains(cookie.domain) {
+ cookieDict[cookie.name] = cookie.properties as AnyObject?
+ }
+ }
+ completion(cookieDict)
+ }
+}}