public static bool checkTokenExpiry() { LoginBAL loginBal = new LoginBAL(AppDelegate.DatabaseContext); Model.AuthenticatedUser authenticatedUser = loginBal.GetUserDetailsFromDatabase(AppDelegate.DatabaseContext); AppDelegate.user = authenticatedUser; bool isUserActiveToken = false; if (authenticatedUser != null && authenticatedUser.UserDetails != null && !string.IsNullOrEmpty(authenticatedUser.UserDetails.Token)) { authenticatedUser.IsTokenActive = CheckIfTokenIsActive(authenticatedUser.UserDetails.ExpiryDate); if (authenticatedUser.IsTokenActive) { isUserActiveToken = true; } } return(isUserActiveToken); }
// // This method is invoked when the application has loaded and is ready to run. In this // method you should instantiate the window, load the UI into it and then make the window // visible. // // You have 17 seconds to return from this method, or iOS will terminate your application. // public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { try { UIApplication.SharedApplication.IdleTimerDisabled = false; databaseContext = CreateDB(); app = this; dataSync = new Sync(databaseContext); //Your View Controller Identifiers defined in Interface Builder String firstViewControllerIdentifier = @"LoginViewController"; String secondViewControllerIdentifier = @"DashBoardViewController"; //check if the key exists and its value LoginBAL loginBal = new LoginBAL(AppDelegate.DatabaseContext); Model.AuthenticatedUser authenticatedUser = loginBal.GetUserDetailsFromDatabase(AppDelegate.DatabaseContext); AppDelegate.user = authenticatedUser; bool isUserDetailsAvailableInDB = false; if (authenticatedUser != null && authenticatedUser.UserDetails != null && !string.IsNullOrEmpty(authenticatedUser.UserDetails.Token)) { authenticatedUser.IsTokenActive = CheckIfTokenIsActive(authenticatedUser.UserDetails.ExpiryDate); isUserDetailsAvailableInDB = true; } //check which view controller identifier should be used String viewControllerIdentifier = (isUserDetailsAvailableInDB && authenticatedUser.IsTokenActive)? secondViewControllerIdentifier : firstViewControllerIdentifier; //IF THE STORYBOARD EXISTS IN YOUR INFO.PLIST FILE AND YOU USE A SINGLE STORYBOARD UIStoryboard storyboard = Window.RootViewController.Storyboard; if (isUserDetailsAvailableInDB && authenticatedUser.IsTokenActive) { startAutoSync(); viewControllerIdentifier = secondViewControllerIdentifier; DashBoardViewController dashBoardViewController = storyboard.InstantiateViewController("DashBoardViewController") as DashBoardViewController; dashBoardViewController.IsFirstLogin = authenticatedUser.IsFirstTimeLoggedIn; DashBoardViewController.Token = authenticatedUser.UserDetails.Token; DashBoardViewController.UserName = string.Concat(authenticatedUser.UserDetails.FirstName, " ", authenticatedUser.UserDetails.LastName); } else if (isUserDetailsAvailableInDB && !authenticatedUser.IsTokenActive) { viewControllerIdentifier = firstViewControllerIdentifier; deleteSession(); UIAlertView alert = new UIAlertView(@"Alert", @"Token Expired", null, NSBundle.MainBundle.LocalizedString("OK", "OK")); alert.Show(); } else { viewControllerIdentifier = firstViewControllerIdentifier; deleteSession(); } //instantiate the view controller UIViewController presentedViewController = storyboard.InstantiateViewController(viewControllerIdentifier); if (presentedViewController is DashBoardViewController) { (presentedViewController as DashBoardViewController).IsIntiatedFromAppDelegate = true; } //UIApplication.SharedApplication.IdleTimerDisabled = true; UINavigationController navController = (UINavigationController)Window.RootViewController; navController.PushViewController(presentedViewController, false); return(true); } catch (Exception ex) { return(true); } }