How to reopen application after login - ios

I'm an android developer. in android, when user login in application, I will re-open the MainActivity class ( controller ) to refresh some views.
in iOS applications, how to do this scenario ?

You can reopen you default/LandingViewController.
Suppose you have a View Controller with name LandingViewController
When you successfully logged in all you need is to re instantiate the LandingViewController
In AppDelegate class make a function with name
func userDidLoggedIn(){
let storyboard = UIStoryboard(name: "Main", bundle: nil)//Replace Main With your own storyboard name containing LandingViewController
let landingViewController = storyboard.instantiateViewController(withIdentifier: "LandingViewControllerIdentifier")//Pass your LandingViewController Identier that you have set in your storyboard file.
guard let subviews = self.window?.subviews else {
return
}
for view in subviews {
view.removeFromSuperview()
}
self.window?.rootViewController = landingViewController
}
Now Simply Call this Function where ever in the entire project like this In your case write below lines in the completion block of login request API.
let delegate = UIApplication.shared.delegate as! AppDelegate
delegate. userDidLoggedIn()

Once user login, you can change your rootviewcontroller like this:
var nav_VC: UIViewController?
func onSuccessfulLogin()
{
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
nav_VC = nil
if nav_VC == nil {
nav_VC = storyboard.instantiateViewController(withIdentifier: "home_nav")
}
self.window?.rootViewController = nav_VC
self.window?.makeKeyAndVisible()
}

Related

Why does pushViewController doesn't work after navigating through Dynamic Link?

I'm building an app that can receive Firebase's Dynamic Link and redirects it into a certain UIViewController after clicking the link. So based on this question that I asked, I have the code in AppDelegate.swift that navigates the app to the UIViewController like this:
func goToDynamicLinkVC() { // This function is in AppDelegate.swift
let destinationStoryboard: UIStoryboard = UIStoryboard(name: "DynamicLink", bundle: nil)
let destinationVC = destinationStoryboard.instantiateViewController(withIdentifier: "DynamicLinkView") as? DynamicLinkVC
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = destinationVC
self.window?.makeKeyAndVisible()
}
And in that UIViewController I also have a function that moves to another UIViewController if a user pressed a button like this:
func routeToDynamicForm() { // This function is in DynamicLinkVC class
let destinationVC = UIStoryboard(name: "DynamicLink", bundle: nil).instantiateViewController(withIdentifier: "DynamicFormView") as! DynamicFormVC
self.navigationController?.pushViewController(destinationVC, animated: true)
}
But weirdly enough now that I pressed the button it doesn't move me to the next View Controller (in this case, DynamicFormVC). I've tried to debug the button whether it's working or not by doing this:
print("button tapped")
It shows the message on the debug area, but it still doesn't redirects to the next view controller. So what can I do to resolve this? If you need more information feel free to ask and I will provide it to you. Thank you.
I changed the goToDynamicLinkVC function to something like this:
func goToDynamicLinkVC() { // This function is in AppDelegate.swift
let destinationStoryboard: UIStoryboard = UIStoryboard(name: "DynamicLink", bundle: nil)
let destinationVC = destinationStoryboard.instantiateViewController(withIdentifier: "DynamicLinkView")
let navController = UINavigationController()
navController.setViewControllers([destinationVC], animated: true)
self.window?.rootViewController = navController
}

Swift Multiple Storyboard - How to access specific one

I had split a couple profiles into multiple storyboards to try to help with my freeze time because I heard to many storyboards can be the cause of why Xcode would just "freeze" for up to 10 mins sometimes causing a crash in my flow of work. So, I need to access each specific storyboard, yet don't know how. This current code accesses the "Main.Storyboard" file yet, I need to call other storyboard files. Here I am checking if a specific profile is signed in and pushing them to the profile. I had all the profiles in one storyboard but now I separated them. Just need to know how to push to other storyboards. Thanks
func checkIfBusinessLoggedIn() {
Auth.auth().addStateDidChangeListener({ (auth, user) in
if (user != nil) {
Database.database().reference().child("Businesses").child((user?.uid)!).observeSingleEvent(of: .value, with: { snapshot in
if snapshot.exists() {
print("Business is Signed In")
let vc = self.storyboard?.instantiateViewController(withIdentifier: "Business Profile")
self.present(vc!, animated: true, completion: nil)
}
})
}
})
}
let storyboard = UIStoryboard(name: "your_storyboard_name", bundle: nil)
vc = storyboard.instantiateViewController(withIdentifier: "your_view_controller_name")
//to push that controller on the stack
self.navigationController?.pushViewController(vc, animated: true)
If your storyboard file is "Business.storyboard", "your_storyboard_name" name should be "Business".
If you want to access other storyboards you need to give that name.
self.storyboard will always point to your default storyboard. Instead do this :
let VC = UIStoryboard(name: "VCStoryBoardName", bundle: nil).instantiateViewController(withIdentifier: "InstructionScreenController") as! VC
Use below code for multipe storybord and create ENUM for storyboard name that is easy to specify storyboard name.
//MARK:- Enum_StoryBoard
enum enumStoryBoard:String {
case main = "Main"
case home = "HomeSB"
}
let storyBoard = UIStoryboard.init(name: enumStoryBoard. home.rawValue, bundle: nil)
let objLocationSearch = storyBoard.instantiateViewController(withIdentifier: "LocationSearch") as? LocationSearch
self.navigationController?.pushViewController(objLocationSearch!, animated: true)
Using enum and generic method to return the required object.
enum AppStoryboard : String {
case First, Second
var instance : UIStoryboard {
return UIStoryboard(name: self.rawValue, bundle: Bundle.main)
}
func instantiateVC<T : UIViewController>(viewControllerClass : T.Type) throws -> T {
let storyboardID = (viewControllerClass as UIViewController.Type).storyboardID
guard let viewObj = instance.instantiateViewController(withIdentifier: storyboardID) as? T else {
throw ExcpectedError.intantiationErro(msg:"ViewController with identifier \(storyboardID)")
}
return viewObj
}
}
use this code to instantiate your viewController
let second = try! AppStoryboard.Second.viewController(viewControllerClass: SecondViewController.self) self.present(second, animated: true, completion: nil)

"Attempt to present while already presenting" still appearing after checks?

let appDelegate = UIKit.UIApplication.shared.delegate!
if let tabBarController = appDelegate.window??.rootViewController as? UITabBarController {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "SignInVC") as! SignInVC
guard !signInVC.isBeingPresented else {
log.warning("Attempt to present sign in sheet when it is already showing")
return
}
signInVC.modalPresentationStyle = UIModalPresentationStyle.formSheet
tabBarController.present(signInVC, animated: true, completion: nil)
}
This code can be called multiple times despite signInVC being presented. I already added this check:
guard !signInVC.isBeingPresented else {
log.warning("Attempt to present sign in sheet when it is already showing")
return
}
but it doesn't seem to prevent this error:
Warning: Attempt to present <App.SignInVC: 0x101f2f280> on <UITabBarController: 0x101e05880> which is already presenting <App.SignInVC: 0x101f4e4c0>
Your guard isn't a valid check. The isBeingPresented is being called on a brand new view controller instance that hasn't yet been presented. So isBeingPresented will always be false. Besides that, that property can only be used from within a view controller's view[Will|Did]Appear method.
What you want to check is to see if the tabBarController has already presented another view controller or not.
And lastly, only create and setup the sign-in view controller if it should be presented.
let appDelegate = UIKit.UIApplication.shared.delegate!
if let tabBarController = appDelegate.window?.rootViewController as? UITabBarController {
if tabBarController.presentedViewController == nil {
let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
let signInVC = storyboard.instantiateViewController(withIdentifier: "SignInVC") as! SignInVC
signInVC.modalPresentationStyle = UIModalPresentationStyle.formSheet
tabBarController.present(signInVC, animated: true, completion: nil)
}
}

iOS how to change rootViewController when dismissController

my storyboard
If user doesn't login, the rootViewController is Login
after user login done, rootViewController is MainTabBarController
I have done that
But, I have encounter question is Logout
My Logout is dismissViewController
If my rootViewController is Login, it works
It will remove current ViewController, so Login appear
But when my rootViewController is MainTabBarController, dismiss is not work, I've try using poptoRootViewController in vain.
what should I do in Logout ?
I want to do like this
dismissController(true,{
rootViewController = `Login`
})
For Logout do following:- (Add below code inside IBAction or didSelect ..etc method where logout is called)
// Making Login as rootViewController as user is no longer logged in
NSUserDefaults.standardUserDefaults().setBool(false, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
let loginVC = self.storyboard?.instantiateViewControllerWithIdentifier("Login") as! loginViewController
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDel.window?.rootViewController = loginVC
Also add following in AppDelegate:-
// Checking user login status, if user already logged in then making main tab bar view controller as root view controller
let userLoginStatus = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
if(userLoginStatus)
{
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewControllerWithIdentifier("MainTabBar") as! ViewController
window!.rootViewController = centerVC
window!.makeKeyAndVisible()
}
And Also where login Validation is done, after validating user credentials:-
#IBAction func loginTapped(sender: AnyObject) {
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let centerVC = mainStoryBoard.instantiateViewControllerWithIdentifier("MainTabBar") as! ViewController
// Important to set status to true
NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")
NSUserDefaults.standardUserDefaults().synchronize()
appDel.window!.rootViewController = centerVC
appDel.window!.makeKeyAndVisible()
}
NOTE:- Don't forget to add STORYBOARD IDs for required View Controllers to instiantiate them
You just need to make a function in AppDelegate and call that function on logout.
eg.(Obj-C Version)
- (void) setCurrentRootController : (UIViewController *)viewController {
[[[UIApplication sharedApplication].delegate window] setRootViewController:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:viewController];
[[[UIApplication sharedApplication].delegate window] setRootViewController:navigation];
}
When you are doing logout you just need to set rootViewController and then call popToRootViewController method.
Hope this will work for you !!
You don't need to use any additional technics to set rootViewController in runtime except following:
UIApplication.sharedApplication().keyWindow?.rootViewController = viewController;
You can wrap this with animation if you want
In Swift You can do like this
let vc: UIViewController! = self.storyboard!.instantiateViewControllerWithIdentifier("LoginViewController")
let window = UIApplication.sharedApplication().windows[0];
window.rootViewController = vc;
You can maintain two windows, one is for login, and second one for the users who got authenticated.
This way you can easily switch btw windows rather than having a messy MVC for it.
Warning, make sure your are in the main thread if you use Delegate or Completion methods.
Code in Swift 3
DispatchQueue.main.async {
guard let tb = self.storyboard?.instantiateViewController(withIdentifier: "LoggedUser") as? UITabBarController else {
print("Could not instantiate view controller with identifier of type LoggedUser")
return
}
self.present(tb, animated: true, completion: nil)}

revealViewController() always returns nil

I'm having some troubles with revealViewController in Xcode 7.2 and iOS 9.2.
My app starts with a view controller embedded in a navigation controller to perform a login.
After login, or if the login token is present, I jump to another view controller embedded in a navigation controller with the following code:
let homePage = self.storyboard?.instantiateViewControllerWithIdentifier("HomeViewController") as! HomeViewController
let homePageNav = UINavigationController(rootViewController: homePage)
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = homePageNav
In this home view controller I would like to have a left navigation menu with SWRealViewController.
I had the SWRealViewController view linked with sw_front to my home navigation controller, and the following code:
if (self.revealViewController() != nil) {
self.menuButton.target = self.revealViewController()
self.menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
But self.revealViewController() always returns nil, so it does not work.
I think I lost the revealViewController somewhere (maybe when I jump from the first navigation controller to the second) but I do not know what to do.
The most convenient to be a reason for the revealViewController to be nil
is you didn't connect segues correctly in stroyboard.
See this tutorial it's quite easy to follow.
Update
If in your case you just need to open a login vc if the user is not logged in you may do like this:
in AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
var rootVCStoryboardId = userIsLoggedin ? "SWRevealViewController" : "LoginViewController"
self.window?.rootViewController = UIStoryboard(name: Storyboards.main, bundle: NSBundle.mainBundle()).instantiateViewControllerWithIdentifier(rootVCStoryboardId)
Where SWRevealViewController is the stroyboard id for SWRevealViewController and LoginViewController is the storyboard id for your login view controller(or its navigation controller if exists).
In case someone is wondering how to do a manual segue, this is what worked for me at the end.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let sw = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController") as! SWRevealViewController
self.view.window?.rootViewController = sw
let destinationController = self.storyboard?.instantiateViewControllerWithIdentifier("StoryboardID") as! NameOfViewController
let navigationController = UINavigationController(rootViewController: destinationController)
sw.pushFrontViewController(navigationController, animated: true)
Incase you are skipping login scene based on the current user information, then make sure to instantiate the storyboard with SWRevealViewController. See below code for reference:
if User.currentUser != nil {
//There is a current user
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SWRevealViewController")
window?.rootViewController = vc
}
else{
//No current user
}

Resources