Getting Black Screen with navigationController?.pushViewController (Swift 3.0) - ios

I am having problems programmatically sending a user from one view controller to another. I am posting the code associated with he flow below. In addition to letting me know what the correct code is (which I would appreciate) I'd also be interested if the logic/design itself seems OK.
I am controlling my UI programmatically. Accordingly, in my app delegate didfinnishlaunchingwithoptions I have
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
FIRApp.configure()
window?.rootViewController = SigninController()
When the user opens the app, they are redirected to the SigninController.
Then, inside of SigninController, I am handling all of the social authentication stuff against Firebase. I have a listener in my code to confirm that the user is (or is not) authenticated and sends him along:
let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFacebookAuth()]
FUIAuth.defaultAuthUI()?.providers = provider
// listen for changes in the authorization state
_authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in
// check if there is a current user
if let activeUser = user {
// check if the current app user is the current FIRUser
if self.user != activeUser {
self.user = activeUser
self.signedInStatus(isSignedIn: true)
print("user session is active, redirecting...")
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)
}
} else {
// user must sign in
self.signedInStatus(isSignedIn: false)
self.loginSession()
}
}
}
In the above code, if the user is confirmed as signed in, then I use the below code to send them along. This is where I am having the problem. Right now I just see a black screen but no error message.
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)
And here is the code for the CUstomTabBarController class.
class CustomTabBarController : UITabBarController
{
override func viewDidLoad()
{
super.viewDidLoad()
let home = createNavController(imageName: "gen-home", rootViewController: HomeController(collectionViewLayout: UICollectionViewFlowLayout()))
let loc = createNavController(imageName: "loc-map-route", rootViewController: LocController(collectionViewLayout: UICollectionViewFlowLayout()))
let stats = createNavController(imageName: "pre-bar-chart", rootViewController: StatsController(collectionViewLayout: UICollectionViewFlowLayout()))
let profile = createNavController(imageName: "account", rootViewController: ProfileController(collectionViewLayout: UICollectionViewFlowLayout()))
viewControllers = [home, loc, stats, profile]
}
private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
{
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
}
I am sure I am overlooking something silly, but sometimes it takes another pair of eyes to point it out.
Thanks in advance.

Here
self.navigationController?.pushViewController(nextViewController, animated: true)
there is no navigationController.
In your AppDelegate you need to do this:
let rootViewController = SigninController()
let navController: UINavigationController = UINavigationController(rootViewController: rootViewController)
FIRApp.configure()
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
It's better to assign rootViewController to the window before making it visible. Otherwise there may be screen blinks.

Related

AppDelegate presents views twice

I'm implementing quick-actions for my app and for that I need to present viewcontrollers from the app delegate. I use code like this:
let viewController = storyboard.instantiateViewController(withIdentifier :"MyControllerID") as! MyController
let navController = UINavigationController.init(rootViewController: viewController)
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(navController, animated: true, completion: nil)
}
If the app is in background and the quickaction is clicked everything works fine, but if the app hasn't been started yet and the quick-action gets clicked the view is presented twice.
What am I doing wrong here?
// Edit 1: This code is in a function and the function is called in applicationDidBecomeActive
In didFinishLaunchingWithOptions I check if the app was launched via Quick-Actions like this:
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
launchedShortcutItem = shortcutItem
}
The launchedShortcutItem is a variable:
var launchedShortcutItem: UIApplicationShortcutItem?
// Edit 2: For those who don't know what I mean by "Quick-Actions" here are the Apple Pages for them: Guideline, Documentation

Opening ViewController In AppDelegate While Keeping Tabbar

In my Xcode project when a user taps on a notification I want to first send them to a certain item in my tabBar then I want to instantiate a view controller and send an object over to that view controller. I have code the that sends them to the tabBar I want, but I do not know how to instantiate them to the view controller while keeping the tabBar and navigation bar connected to the view controller. All the answers on this require you to change the root view controller and that makes me lose connection to my tabBar and navigation bar when the view controller is called.
A Real Life Example of this: User receives Instagram notification saying "John started following you" -> user taps on notification -> Instagram opens and shows notifications tab -> quickly send user to "John" profile and when the user presses the back button, it sends them back to the notification tab
Should know: The reason why I'm going to a certain tab first is to get that tab's navigation controller because the view controller I'm going to does not have one.
Here's my working code on sending the user to "notifications" tab (I added comments to act like the Instagram example for better understanding):
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[3] //goes to notifications tab
if type == "follow" { //someone started following current user
//send to user's profile and send the user's id so the app can find all the information of the user
}
}
First of all, you'll to insatiate a TabBarController:
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
And then insatiate all of the viewControllers of TabBarController. If your viewControllers is embedded in to the UINavigationController? If so, you'll to insatiate a Navigation Controller instead:
let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController
Also you should instantiate your desired ViewController too:
let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController
Make all of the NavigationControllers as viewControllers of TabBarController:
tabBarController.viewControllers = [first, second, third]
And check: It's about your choice.
if tabBarController.selectedViewController == first {
// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)
// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)
}
Make tabBarController as a rootViewController:
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
Finally: It's your completed code:
func openViewController() {
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController
let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController
tabBarController.viewControllers = [first, second, third]
if tabBarController.selectedViewController == first {
// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)
// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
If you want to present or push ViewController when the notification is tapped? Try something like that:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
openViewController()
completionHandler()
default:
break;
}
}
}
I can think of two ways to do that:
1) If that view controller is a UINavigationController you can simply push the profile from wherever you are:
if let tabNavigationController = tabbarController.viewControllers?[3] as? UINavigationController {
tabbarController.selectedViewController = tabNavigationController
let profileViewController = ProfileViewController(...)
// ... set up the profile by setting the user id or whatever you need to do ...
tabNavigationController.push(profileViewController, animated: true) // animated or not, your choice ;)
}
2) Alternatively, what I like to do is control such things directly from my view controller subclass (in this case, PostListViewController). I have this helper method in a swift file that I include in all of my projects:
extension UIViewController {
var containedViewController: UIViewController {
if let navController = self as? UINavigationController, let first = navController.viewControllers.first {
return first
}
return self
}
}
Then I would do this to push the new view controller:
if let tabViewController = tabbarController.selectedViewController {
tabbarController.selectedViewController = tabViewController
if let postListViewController = tabViewController.containedViewController as? PostListViewController {
postListViewController.goToProfile(for: user) // you need to get the user reference from somewhere first
}
}
In my last live project, I'm using the same approach like yours. So even though I doubt this method is the correct or ideal for handling a push notification from the AppDelegate (I still got a lot of stuff to learn in iOS 🙂), I'm still sharing it because it worked for me and well I believe the code is still readable and quite clean.
The key is to know the levels or stacks of your screens. The what are childViewControllers, the topMost screen, the one the is in the bottom, etc...
Then if you're now ready to push to a certain screen, you would need of course the navigationController of the current screen you're in.
For instance, this code block is from my project's AppDelegate:
func handleDeeplinkedJobId(_ jobIdInt: Int) {
// Check if user is in Auth or in Jobs
if let currentRootViewController = UIApplication.shared.keyWindow!.rootViewController,
let presentedViewController = currentRootViewController.presentedViewController {
if presentedViewController is BaseTabBarController {
if let baseTabBarController = presentedViewController as? BaseTabBarController,
let tabIndex = TabIndex(rawValue: baseTabBarController.selectedIndex) {
switch tabIndex {
case .jobsTab:
....
....
if let jobsTabNavCon = baseTabBarController.viewControllers?.first,
let firstScreen = jobsTabNavCon.childViewControllers.first,
let topMostScreen = jobsTabNavCon.childViewControllers.last {
...
...
So as you can see, I know the hierarchy of the screens, and by using this knowledge as well as some patience in checking if I'm in the right screen by using breakpoints and printobject (po), I get the correct reference. Lastly, in the code above, I have the topMostScreen reference, and I can use that screen's navigationController to push to a new screen if I want to.
Hope this helps!

Swift - Is this code wrong or should I use UserDefaults?

I have an app which is connected to Firebase and you can login using Google and Facebook. The app has a home screen which is loaded when the user is signed in. I don't want to show the login screen if the user has already signed in before. So I use this code in the home screen to check if the user is signed in. Home screen is the initial view controller in the app
override func viewDidAppear(_ animated: Bool) {
if Auth.auth().currentUser != nil{
// user is signed in
}else{
// No user is signed in.
performSegue(withIdentifier: "login", sender: nil)
}
}
This code works very well. I know, that I could use UserDefaults too, but this seems to be an easier way. Is it wrong or should I use UserDefaults?
But the only problem with that is, for a quick moment the home screen is shown and then the segue is performed to the login screen, and I guess it's because it's in the viewdidApepar method, so the code gets executed after the viewcontroller appeared. Do you know a solution to it?
This is what I do personally, didFinishLaunchingWithOptions in AppDelegate.
if UserManager.sharedManager.currentUser != nil {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let viewController = storyboard.instantiateInitialViewController()
appDelegate.window?.rootViewController = viewController
} else {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let storyboard = UIStoryboard(name: "Login", bundle: Bundle.main)
let viewController = storyboard.instantiateInitialViewController()
appDelegate.window?.rootViewController = viewController
}
Of course I have a UserManager which handles if the user is logged in or not.(UserManager:NSObject)
var currentUser: User? {
return User.unarchiveStoredUser()
}
User is stored with decoder into UserDefautls then unarchived to verify if a user is logged in or not (User :NSObject)
func archiveUser() {
let data = NSKeyedArchiver.archivedData(withRootObject: self)
UserDefaults.standard.set(data, forKey: "userIdentifier")
UserDefaults.standard.synchronize()
}
class func unarchiveStoredUser() -> User? {
if let data = UserDefaults.standard.object(forKey: "userIdentifier") as? Data {
let user = NSKeyedUnarchiver.unarchiveObject(with: data) as! User
return user
}
return nil
}
Hope it helps you.

Changing rootViewController causing weird behavior

I am setting rootViewController like this in my app.
func setupMainView() {
let rootViewContorller = window?.rootViewController
if (rootViewContorller?.presentedViewController != nil || rootViewContorller?.presentingViewController != nil) {
rootViewContorller?.dismiss(animated: false, completion: nil)
}
let tabbarController = UITabBarController()
tabbarController.delegate = self
let homeViewController = HomeViewController()
let rewardsViewController = RewardsViewController()
let homeNVc = UINavigationController()
let rewardsNVc = UINavigationController()
homeNVc.viewControllers = [homeViewController]
rewardsNVc.viewControllers = [rewardsViewController]
tabbarController.viewControllers = []
tabbarController.viewControllers = [homeNVc, rewardsNVc]
tabbarController.selectedIndex = 0
self.window?.rootViewController = tabbarController
}
It is working fine. But I have to change rootViewController in the app like after registration etc. After that When I go to Debug View Hierarchy . I still see the registrationViewController there. And lets say If I change rootViewController 3-4 times all previous controllers are still there. So my question is How can I remove all viewControllers from memory before changing the rootViewController.
You can call this function this will clear all viewcontrollers.
self.window?.rootViewController?.dismissViewControllerAnimated(false, 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)}

Resources