iOS Swift: Pushing two view controllers when app launched from push notification - ios

When a user launches the app from a push notification I present one view controller, then push another. My code to present the first VC is as follows
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeNav: UIViewController = mainStoryBoard.instantiateViewController(withIdentifier: "HomeNavController") as! UINavigationController
let homePageTableVC = mainStoryBoard.instantiateViewController(withIdentifier: String(describing: HomePageTableViewController.self)) as! HomePageTableViewController
homePageTableVC.tipToPresent = tipDay
homeNav.addChildViewController(homePageTableVC)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = homeNav
self.window?.makeKeyAndVisible()
The tipToPresent property is used by the homePageTableVC (in the viewDidLoad method) to then present the second VC, using this code:
if let tipDayToPresent = tipToPresent {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tipVC = storyboard.instantiateViewController(withIdentifier: String(describing: TipViewController.self)) as! TipViewController
tipVC.dayOfTip = tipDayToPresent
tipToPresent = nil
navigationController?.pushViewController(tipVC, animated: true)
}
This works well, but when I press the back button to return to the homePageTableVC, the navigation bar is blank. The title image, menu button, and right bar button that normally show up are not visible.
Any help is appreciated.

Good if solution by #augie works. I would suggest you should not change navigation stack when you handle push notification deep link. It should behave same as it does in normal app launch. By that way you don't need to handle any edge case and no need to set up different window.
Solution: Whenever someone click on push notification dismiss all presented controller and popToRootViewController and then navigate to desired screen.

Can you try changing this
homeNav.addChildViewController(homePageTableVC)
to this
homeNav.setViewControllers([homePageTableVC], animated: false)

I believe the problem is how you are adding your table on the navigation controller. Instead of making it your navigation's root viewcontroller you are adding it as a child vc.
Changing your code like this should work:
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
// Remove type UIViewController
let homeNav = mainStoryBoard.instantiateViewController(withIdentifier: "HomeNavController") as! UINavigationController
let homePageTableVC = mainStoryBoard.instantiateViewController(withIdentifier: String(describing: HomePageTableViewController.self)) as! HomePageTableViewController
homePageTableVC.tipToPresent = tipDay
// Set controllers instead of adding child
homeNav.setViewControllers([homePageTableVC], animated: false)
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = homeNav
self.window?.makeKeyAndVisible()

Related

how to set view controller as a rootviewcontroller

I have two classes AppDelegate.swift and scene.swift and My first screen is login screen when I click login button then it should set my HomeVC.swift as rootViewController at the action of login button I mentioned this code and its not giving any error not even running
let sceneDelegate = SceneDelegate()
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = mainStoryboard.instantiateViewController(withIdentifier: "HomeVC") as! HomeVC
sceneDelegate.window?.rootViewController = vc
window?.makeKeyAndVisible()
Can somebody guide me how to work with new file sceneDelegate.swift?
Thanks in advance
when I click login button then it should set my HomeVC.swift as rootViewController
let vc = // ...
self.view.window?.rootViewController = vc

Why does Navigation Controller disappear when Paper-Onboarding is launched upon app installation?

I have this app that requires to have OnboardingVC which provides tutorials for the users. The OnboardingVCis launched upon app installation. I added my code inside appdelegate which consist of code below:
`if !UserDefaults.standard.bool(forKey: "didSee") {
UserDefaults.standard.set(true, forKey: "didSee")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainController = storyboard.instantiateViewController(withIdentifier: "OnBoardingVC")
self.window?.rootViewController = mainController
self.window?.makeKeyAndVisible()
}`
When I tapped the Skip and Get Started buttons inside OnboardingVC to proceed to the MainViewController, the Navigation Bar disappears as well as with the other ViewControllers. But when I tapped the Login Button inside MainViewController the Navigation Bar works smoothly. Below are screenshot of my storyboard
Hope I did explain it in a way you can understand my issue. Please help me. Thank you
Use something like this
as your app always start with a navigation controller
if !UserDefaults.standard.bool(forKey: "didSee") {
UserDefaults.standard.set(true, forKey: "didSee")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let mainController = storyboard.instantiateViewController(withIdentifier: "OnBoardingVC")
let nav1 = UINavigationController()
nav1.viewControllers = [mainController] //Set on board vc as rootviewcontroller
self.window?.rootViewController = nav1
self.window?.makeKeyAndVisible()
}`

Swipe Back Gesture After Login in Swift

In my application, after the user is successfully logged in via his email and password in the first storyboard (Auth.storyboard), he is directed to one of the ViewControllers in the second storyboard (Main.storyboard). The problem is that user is able to swipe back to the login screen in Auth.storyboard.
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
I know that with the above code it is possible to disable this swipe back gesture but according to most people it is not recommended.
Therefore I wonder that is there a better solution to prevent swipe back gesture after a user logged in.
The best way is to present your login controller modally, then dismiss it to don't add this controller in your navigation stack.
If it's your initial controller, embed it in a different controller than the next one or remove it from navigation stack programmatically.
You have to remove the Login ViewController from the stack and you can also make your home view controller as a rootViewController:
var mainNavigationController:UINavigationController?
//After login success
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeInstance = mainStoryboard.instantiateViewControllerWithIdentifier("HomeVC")
mainNavigationController = UINavigationController(rootViewController: homeInstance)
mainNavigationController?.navigationBar.hidden = true
self.window?.rootViewController = mainNavigationController
//Appdelegate code didFinishLaunching
if getUserDefault("isUser") == "YES" {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeInstance = mainStoryboard.instantiateViewControllerWithIdentifier("HomeVC")
mainNavigationController = UINavigationController(rootViewController: homeInstance)
mainNavigationController?.navigationBar.hidden = true
self.window?.rootViewController = mainNavigationController
}
else {
mainNavigationController = window!.rootViewController as? UINavigationController
}
When the login button is clicked you must have pushed the view controller to another screen.
Try to set the view controller instead of push:
func Login(){
let control = storyboard!.instantiateViewController(withIdentifier: identifier)
navigationController?.setViewControllers([control], animated: true)
}
If you were pushing your new controller ,then instead of pushing try adding that to keywindow like this . You will not be redirected to previous window then.
let homeStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let vc = homeStoryBoard.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
if let window = appDelegate.window {
window.rootViewController = vc
window.makeKeyAndVisible()
}

Swift showing view only on first launch with navigation controller

I have some code to only show the first view controller in my storyboard on the first launch of the app. After that I want to skip that page and go straight to my second view on each launch. I have embedded the first view (which is connected to the second) in a navigation controller.
My issue is that after the first launch when the app goes to the second view directly it's showing the view without the navigation bar on top and I'm not sure why.
In my appdelegate:
func firstLaunchCheck(){
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore{
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialView : UIViewController = storyboard.instantiateViewController(withIdentifier: "mainScreen") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialView
self.window?.makeKeyAndVisible()
}
else{
UserDefaults.standard.set(true, forKey: "launchedBefore")
}
}
UPDATE:
I wound up just changing which view controller were embedded in the navigation controller (excluded the first one) since it didn't make sense to me to have it there. So now after the first launch it loads the navigation controller
SecondViewController is not added in UINavigationController hierarchy, to see the navigationBar on top you can push SecondViewController on firstVC if the launchedBefore is false in appDelegate
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let secondVC = storyboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
let navigationController = window.rootViewController as! UINavigationController
navigationController?.pushViewController(secondVC, animated: false)
You need to embed the second view controller i.e. "mainScreen" in UINavigationController and then make it the rootViewController of your app window.
let storyboard : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = UINavigationController.init(rootViewController: storyboard.instantiateViewController(withIdentifier: "mainScreen"))
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialView
self.window?.makeKeyAndVisible()

Swift programmatically navigate to another view controller/scene

I'm using following code to programmatically navigate to another ViewController. It works fine, but it some how hides the navigation bar. How do I fix this? (the navigation bar is created by embeding the ViewController in the navigation controller if that matters.)
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("nextView") as NextViewController
self.presentViewController(nextViewController, animated:true, completion:nil)
Swift 5
The default modal presentation style is a card. This shows the previous view controller at the top and allows the user to swipe away the presented view controller.
To retain the old style you need to modify the view controller you will be presenting like this:
newViewController.modalPresentationStyle = .fullScreen
This is the same for both programmatically created and storyboard created controllers.
Swift 3
With a programmatically created Controller
If you want to navigate to Controller created Programmatically, then do this:
let newViewController = NewViewController()
self.navigationController?.pushViewController(newViewController, animated: true)
With a StoryBoard created Controller
If you want to navigate to Controller on StoryBoard with Identifier "newViewController", then do this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "newViewController") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
SWIFT 4.x
The Strings in double quotes always confuse me, so I think answer to this question needs some graphical presentation to clear this out.
For a banking app, I have a LoginViewController and a BalanceViewController. Each have their respective screens.
The app starts and shows the Login screen. When login is successful, app opens the Balance screen.
Here is how it looks:
The login success is handled like this:
let storyBoard: UIStoryboard = UIStoryboard(name: "Balance", bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "balance") as! BalanceViewController
self.present(balanceViewController, animated: true, completion: nil)
As you can see, the storyboard ID 'balance' in small letters is what goes in the second line of the code, and this is the ID which is defined in the storyboard settings, as in the attached screenshot.
The term 'Balance' with capital 'B' is the name of the storyboard file, which is used in the first line of the code.
We know that using hard coded Strings in code is a very bad practice, but somehow in iOS development it has become a common practice, and Xcode doesn't even warn about them.
You should push the new viewcontroller by using current navigation controller, not present.
self.navigationController.pushViewController(nextViewController, animated: true)
According to #jaiswal Rajan in his answer. You can do a pushViewController like this:
let storyBoard: UIStoryboard = UIStoryboard(name: "NewBotStoryboard", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "NewViewController") as! NewViewController
self.navigationController?.pushViewController(newViewController, animated: true)
So If you present a view controller it will not show in navigation controller. It will just take complete screen. For this case you have to create another navigation controller and add your nextViewController as root for this and present this new navigationController.
Another way is to just push the view controller.
self.presentViewController(nextViewController, animated:true, completion:nil)
For more info check Apple documentation:-
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/doc/uid/TP40006926-CH3-SW96
OperationQueue.main.addOperation {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Storyboard ID") as! NewViewController
self.present(newViewController, animated: true, completion: nil)
}
It worked for me when I put the code inside of the OperationQueue.main.addOperation, that will execute in the main thread for me.
All other answers sounds good, I would like to cover my case, where I had to make an animated LaunchScreen, then after 3 to 4 seconds of animation the next task was to move to Home screen. I tried segues, but that created problem for destination view. So at the end I accessed AppDelegates's Window property and I assigned a new NavigationController screen to it,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
//Below's navigationController is useful if u want NavigationController in the destination View
let navigationController = UINavigationController(rootViewController: homeVC)
appDelegate.window!.rootViewController = navigationController
If incase, u don't want navigationController in the destination view then just assign as,
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let homeVC = storyboard.instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
appDelegate.window!.rootViewController = homeVC
The above code works well but if you want to navigate from an NSObject class, where you can not use self.present:
let storyBoard = UIStoryboard(name:"Main", bundle: nil)
if let conVC = storyBoard.instantiateViewController(withIdentifier: "SoundViewController") as? SoundViewController,
let navController = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
navController.pushViewController(conVC, animated: true)
}

Resources