100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
iOS console : reachability, error handling
William Balcaen021f7110f795
diff --git a/console/iOS/console/AppDelegate.swift b/console/iOS/console/AppDelegate.swiftindex 877cc6490..a56138568 100644--- a/console/iOS/console/AppDelegate.swift+++ b/console/iOS/console/AppDelegate.swift@@ -50,30 +50,40 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDfunc applicationWillResignActive(_ application: UIApplication) {// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.+ reachabilityAlert?.dismiss(animated: true, completion: nil)+ reachabilityAlertShown = falseinternetReachability?.stopNotifier()}func applicationDidEnterBackground(_ application: UIApplication) {// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.+ reachabilityAlert?.dismiss(animated: true, completion: nil)+ reachabilityAlertShown = falseinternetReachability?.stopNotifier()}func applicationWillEnterForeground(_ application: UIApplication) {// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.- internetReachability?.startNotifier()- self.updateReachabilityStatus(reachability: internetReachability!)+ DispatchQueue.main.asyncAfter(deadline: .now() + 3) {+ self.internetReachability?.startNotifier()+ self.updateReachabilityStatus(reachability: self.internetReachability!)+ }}func applicationDidBecomeActive(_ application: UIApplication) {// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.connectToFcm()- internetReachability?.startNotifier()- self.updateReachabilityStatus(reachability: internetReachability!)+ DispatchQueue.main.asyncAfter(deadline: .now() + 3) {+ self.internetReachability?.startNotifier()+ self.updateReachabilityStatus(reachability: self.internetReachability!)+ }}func applicationWillTerminate(_ application: UIApplication) {// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.+ reachabilityAlert?.dismiss(animated: true, completion: nil)+ reachabilityAlertShown = falseinternetReachability?.stopNotifier()}diff --git a/console/iOS/console/ORViewController.swift b/console/iOS/console/ORViewController.swiftindex ae3056127..893ca07e6 100644--- a/console/iOS/console/ORViewController.swift+++ b/console/iOS/console/ORViewController.swift@@ -56,7 +56,11 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa}func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {- let alertView = UIAlertController(title: "debug", message: message, preferredStyle: .alert)+ NSLog("error %@", message)+ let error = NSError(domain: "", code: 0, userInfo: [+ NSLocalizedDescriptionKey : NSLocalizedString("ErrorGettingLoginScreen", value: "Could not login, your access token probably expired", comment: "")+ ])+ let alertView = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)let alertAction = UIAlertAction(title: "Done", style: .cancel) { (action) incompletionHandler()}@@ -105,7 +109,11 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHalet reqDataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) inDispatchQueue.main.async {if (error != nil) {- self.showError(error: error!)+ NSLog("error %@", (error as! NSError).localizedDescription)+ let error = NSError(domain: "", code: 0, userInfo: [+ NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingAPI", value: "Could not get data", comment: "")+ ])+ self.showError(error: error)} else {_ = self.myWebView?.load(data!, mimeType: "text/html", characterEncodingName: "utf8", baseURL: URL(string:Server.apiTestResource)!)}@@ -114,19 +122,33 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHareqDataTask.resume()} else {if let httpResponse = response as? HTTPURLResponse {- let error = NSError(domain: "", code: httpResponse.statusCode, userInfo: jsonDictionnary)+ NSLog("error %@", (error as! NSError).localizedDescription as String)+ let error = NSError(domain: "", code: httpResponse.statusCode, userInfo: [+ NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingAPI", value: "Could not get data", comment: "")+ ])self.showError(error: error)} else {- let error = NSError(domain: "", code: 0, userInfo: jsonDictionnary)+ NSLog("error %@", (error as! NSError).localizedDescription as String)+ let error = NSError(domain: "", code: 0, userInfo: [+ NSLocalizedDescriptionKey : NSLocalizedString("ErrorCallingAPI", value: "Could not get data", comment: "")+ ])self.showError(error: error)}}}catch let error as NSError {+ NSLog("error %@", error.localizedDescription)+ let error = NSError(domain: "", code: 0, userInfo: [+ NSLocalizedDescriptionKey : NSLocalizedString("ErrorDeserializingJSON", value: "Could not convert received data", comment: "")+ ])self.showError(error: error)}} else {- self.showError(error: error!)+ NSLog("error %@", (error as! NSError).localizedDescription)+ let error = NSError(domain: "", code: 0, userInfo: [+ NSLocalizedDescriptionKey : NSLocalizedString("NoDataReceived", value: "Did not receive any data", comment: "")+ ])+ self.showError(error: error)}})req.resume()@@ -138,7 +160,10 @@ class ORViewcontroller : UIViewController, URLSessionDelegate, WKScriptMessageHa}func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {- self.showError(error: error)+ NSLog("error %@", error.localizedDescription)+ showError(error: NSError(domain: "networkError", code: 0, userInfo:[+ NSLocalizedDescriptionKey : NSLocalizedString("FailedLoadingPage", value: "Could not load page", comment: "")+ ]))}}diff --git a/console/iOS/console/TokenManager.swift b/console/iOS/console/TokenManager.swiftindex d2df033a8..fb6cf4321 100644--- a/console/iOS/console/TokenManager.swift+++ b/console/iOS/console/TokenManager.swift@@ -119,17 +119,25 @@ class TokenManager:NSObject, WKScriptMessageHandler, WKUIDelegate, WKNavigationDalertView.addAction(alertAction)viewController.present(alertView, animated: true, completion: nil)}+func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {- showError(error: error)+ NSLog("error %@", error.localizedDescription)+ showError(error: NSError(domain: "networkError", code: 0, userInfo:[+ NSLocalizedDescriptionKey : NSLocalizedString("FailedLoadingPage", value: "Could not load page", comment: "")+ ]))}+func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {print("navigation failed %@",error)let errorCode = (error as NSError).codeif errorCode != NSURLErrorCancelled {- showError(error: error)-+ NSLog("error %@", error.localizedDescription)+ showError(error: NSError(domain: "navigationError", code: 0, userInfo:[+ NSLocalizedDescriptionKey : NSLocalizedString("navigationError", value: "Could not navigate to page", comment: "")+ ]))}}+func showError(error : Error) {print("showing error %@",error)let alertVC = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)diff --git a/console/iOS/console/TokenManagerViewController.swift b/console/iOS/console/TokenManagerViewController.swiftindex 7f5f31162..7375ec986 100644--- a/console/iOS/console/TokenManagerViewController.swift+++ b/console/iOS/console/TokenManagerViewController.swift@@ -11,4 +11,7 @@ import UIKitclass TokenManagerViewController: UIViewController {+ override func viewDidLoad() {+ self.view.frame = UIScreen.main.bounds+ }}