Skip to content

armgit / core

Public Imported from GitHub
0

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

iOS console : fcm notifications, code cleaning

William Balcaen1e7ac0a940fa
diff --git a/console/iOS/NotificationExtension/NotificationService.swift b/console/iOS/NotificationExtension/NotificationService.swift
index 7a31bf4c6..21d8150e0 100644
--- a/console/iOS/NotificationExtension/NotificationService.swift
+++ b/console/iOS/NotificationExtension/NotificationService.swift
@@ -48,7 +48,7 @@ class NotificationService: UNNotificationServiceExtension, URLSessionDelegate {
bestAttemptContent.body = error.debugDescription
} else {
do {
- print(String(data: data!, encoding: .utf8) ?? "")
+ //print(String(data: data!, encoding: .utf8) ?? "")
let json = try JSONSerialization.jsonObject(with: data!) as? [[String: Any]]
if (json?.count)! > 0 {
let detailedJson = (json?[0])! as [String: Any]
diff --git a/console/iOS/console/ORViewController.swift b/console/iOS/console/ORViewController.swift
index 0f8dda749..fba9b75d6 100644
--- a/console/iOS/console/ORViewController.swift
+++ b/console/iOS/console/ORViewController.swift
@@ -20,6 +20,10 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = UIColor.white
+ }
+
+ override func viewWillAppear(_ animated: Bool) {
+ super.viewWillAppear(animated)
self.configureAccess()
}
@@ -47,18 +51,42 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
}
}
+ func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
+ if let response = navigationResponse.response as? HTTPURLResponse {
+ if response.statusCode != 200 && response.statusCode != 204 {
+ decisionHandler(.cancel)
+ NSLog("error http status : %@ message : %@",response.statusCode,response.description)
+ let error = NSError(domain: "", code: 0, userInfo: [
+ NSLocalizedDescriptionKey : NSLocalizedString("HTTPErrorReturned", value: "Connection Error", comment: "")
+ ])
+ if let vc = self.presentingViewController as? ViewController {
+ vc.isInError = true
+ }
+ self.dismiss(animated: true) {
+ ErrorManager.showError(error:error)
+ }
+ }
+ }
+ decisionHandler(.allow)
+ }
+/*
// just for testing purpose (ask backend to get list of notifications or send a notification to device)
func apiCall() {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
TokenManager.sharedInstance.getAccessToken { (accessTokenResult) in
switch accessTokenResult {
case .Failure(let error) :
- ErrorManager.showError(error: error!)
+ UIApplication.shared.isNetworkActivityIndicatorVisible = false
+ print(error ?? "")
+ ErrorManager.showError(error: NSError(domain: "", code: 0, userInfo: [
+ NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingapi", value: "Delete app and restart token probably expired", comment: "")
+ ]))
+
case .Success(let accessToken) :
guard let urlRequest = URL(string: String(Server.apiTestResource)) else { return }
let request = NSMutableURLRequest(url: urlRequest)
- request.httpMethod = "GET" // post to create a notification alert, get to get a list of notification alerts
+ request.httpMethod = "POST" // post to create a notification alert, get to get a list of notification alerts
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = "{\"title\" :\" notif alert title\",\"message\" : \"notif alert message\",\"appUrl\" : \"Veilig\",\"actions\": [ {\"title\" : \"Open link\" , \"type\": \"ACTION_DEEP_LINK\"},{\"title\" : \"background call\" , \"type\": \"ACTION_ACTUATOR\" , \"assetId\" : \"2E2vrICRSEa6mfzOquZzxA\", \"attributeName\" : \"targetTemp\", \"rawJson\" : \"18\" } ]}".data(using: .utf8)
request.addValue(String(format:"Bearer %@", accessToken!), forHTTPHeaderField: "Authorization")
@@ -83,7 +111,7 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
}
}
}
-
+*/
func login() {
guard let request = URL(string: String(format:"https://%@/%@",Server.hostURL,Server.initialPath)) else { return }
UIApplication.shared.isNetworkActivityIndicatorVisible = true
@@ -93,9 +121,14 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
NSLog("error %@", error.localizedDescription)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
- ErrorManager.showError(error: NSError(domain: "networkError", code: 0, userInfo:[
- NSLocalizedDescriptionKey : NSLocalizedString("FailedLoadingPage", value: "Could not load page", comment: "")
- ]))
+ if let vc = self.presentingViewController as? ViewController {
+ vc.isInError = true
+ }
+ self.dismiss(animated: true) {
+ ErrorManager.showError(error: NSError(domain: "networkError", code: 0, userInfo:[
+ NSLocalizedDescriptionKey : NSLocalizedString("FailedLoadingPage", value: "Could not load page in OR", comment: "")
+ ]))
+ }
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
@@ -161,10 +194,11 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
TokenManager.sharedInstance.getAccessToken { (accessTokenResult) in
switch accessTokenResult {
case .Failure(let error) :
+ UIApplication.shared.isNetworkActivityIndicatorVisible = false
ErrorManager.showError(error: error!)
case .Success(let accessToken) :
guard let urlRequest = URL(string: String(String(format: "https://%@/%@/asset/%@/attribute/%@", Server.hostURL, Server.realm, assetId,attributeName))) else { return }
- print(urlRequest)
+ //print(urlRequest)
let request = NSMutableURLRequest(url: urlRequest)
request.httpMethod = "PUT"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
@@ -181,8 +215,6 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa
NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingAPI", value: "Could not get data", comment: "")
])
ErrorManager.showError(error: error)
- } else {
- print(response.debugDescription)
}
}
})
diff --git a/console/iOS/console/TokenManager.swift b/console/iOS/console/TokenManager.swift
index 16babff87..6f366ab00 100644
--- a/console/iOS/console/TokenManager.swift
+++ b/console/iOS/console/TokenManager.swift
@@ -145,9 +145,15 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
NSLog("error %@", error.localizedDescription)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
- ErrorManager.showError(error: NSError(domain: "networkError", code: 0, userInfo:[
- NSLocalizedDescriptionKey : NSLocalizedString("FailedLoadingPage", value: "Could not load page", comment: "")
- ]))
+ if let vc = viewController.presentingViewController as? ViewController {
+ vc.isInError = true
+ }
+ self.viewController.dismiss(animated: true) {
+ ErrorManager.showError(error: NSError(domain: "networkError", code: 0, userInfo:[
+ NSLocalizedDescriptionKey : NSLocalizedString("FailedLoadingPage", value: "Could not load page", comment: "")
+ ]))
+ }
+
}
func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
@@ -161,6 +167,25 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
}
}
+ func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
+ if let response = navigationResponse.response as? HTTPURLResponse {
+ if response.statusCode != 200 && response.statusCode != 204 {
+ decisionHandler(.cancel)
+ NSLog("error http status : %i ",response.statusCode)
+ let error = NSError(domain: "", code: 0, userInfo: [
+ NSLocalizedDescriptionKey : NSLocalizedString("HTTPErrorReturned", value: "Connection Error", comment: "")
+ ])
+ if let vc = viewController.presentingViewController as? ViewController {
+ vc.isInError = true
+ }
+ self.viewController.dismiss(animated: true) {
+ ErrorManager.showError(error: error)
+ }
+ }
+ }
+ decisionHandler(.allow)
+ }
+
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
let errorCode = (error as NSError).code
UIApplication.shared.isNetworkActivityIndicatorVisible = false
@@ -183,11 +208,11 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
}
func sendDeviceId() {
- print("Device Id : -------------> ",deviceId ?? "")
UIApplication.shared.isNetworkActivityIndicatorVisible = true
self.getAccessToken { (accessTokenResult) in
switch accessTokenResult {
case .Failure(let error) :
+ UIApplication.shared.isNetworkActivityIndicatorVisible = false
ErrorManager.showError(error: error!)
case .Success(let accessToken) :
guard let urlRequest = URL(string: String(Server.registerDeviceResource)) else { return }
@@ -233,6 +258,7 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationD
tkRequest.addValue("application/x-www-form-urlencoded", forHTTPHeaderField:"Content-Type");
tkRequest.httpMethod = "POST"
let postString = String(format:"grant_type=refresh_token&refresh_token=%@&client_id=%@",(TokenManager.sharedInstance.refreshToken )!,Client.clientId)
+
tkRequest.httpBody = postString.data(using: .utf8)
let sessionConfiguration = URLSessionConfiguration.default
let session = URLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil)
diff --git a/console/iOS/console/ViewController.swift b/console/iOS/console/ViewController.swift
index 23595aab8..b83c56dd6 100644
--- a/console/iOS/console/ViewController.swift
+++ b/console/iOS/console/ViewController.swift
@@ -26,9 +26,9 @@ class ViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if isInError {
- loginButton.isHidden = true
- } else {
loginButton.isHidden = false
+ } else {
+ loginButton.isHidden = true
}
}