Viewcontroller just shows black screen after implementing a rootviewcontroller - ios

I am pretty new to programming, thats why I can't really figure out how to solve this issue.
I have implemented a rootviewcotnroller in the app delegate so that if the user is logged in he is pushed directly to the app content instead of the login view controller.
However it doesn't really work. As is already said I added the following code to the app delegate:
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = MainViewController()
The MainViewcontroller is set up like this:
class MainViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = UserViewController()
viewControllers = [homeController]
}else {
perform(#selector(showLoginController), with: nil, afterDelay: 0.01)
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.isLoggedIn()
}
func showLoginController() {
let loginController = LoginViewController()
present(loginController, animated: true, completion: {
})
}
}
To the Userviewcontroller I have added the following lines:
func handleSignout() {
UserDefaults.standard.setisLoggedIn(value: false)
print("is logged out")
}
#IBAction func SignOut(_ sender: Any) {
handleSignout()
if FIRAuth.auth()!.currentUser != nil {
do {
try? FIRAuth.auth()?.signOut()
if FIRAuth.auth()?.currentUser == nil {
let loginViewViewcontroller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Login") as! LoginViewController
self.present(loginViewViewcontroller, animated: true, completion: nil)
}
}
}
}
Then I have created an extension with the UserDefaults to save the boolean Value whether the user is logged in or logged out:
extension UserDefaults {
enum UserDefaultKeys: String {
case isLoggedIn
}
func setisLoggedIn(value: Bool) {
set(false, forKey: UserDefaultKeys.isLoggedIn.rawValue)
synchronize()
}
func isLoggedIn() -> Bool {
return bool(forKey: UserDefaultKeys.isLoggedIn.rawValue)
}
}
In the LoginviewController, which just shows a black screen if shown at first sight, I have added :
func finishLoggingIn() {
let rootViewController = UIApplication.shared.keyWindow?.rootViewController
guard let MainNavigationController = rootViewController as? MainViewController else {return}
MainNavigationController.viewControllers = [UserViewController()]
print("is logged in")
UserDefaults.standard.setisLoggedIn(value: true)
dismiss(animated: true, completion: nil)
}
This function is called when user pushes the login button.
The app recognizes if the user is logged or not, but it doesn't matter if the user is logged in or not, that first view controller which is presented shows a black screen, which is most likely the loginviewcontroller but if the user is logged in the userviewcontroller shows a black screen as well if is the first view controller to be presented. ...
Has anybody an idea why that is?

I have figured out how to solve it. It is way easier then I expected it to be. Maybe the solution I've found is not the best way of doing it, but as long as it works I am happy!
I deleted all function with the UserDefaults.
To my MainViewController I have just added a the following code, which is suggested by Firebase itself:
import UIKit
import FirebaseAuth
// DIeser Viewcontroller wird immer als erstes aufgerufen und entscheidet, ob der loginviewcontroller gestartet wird oder der UserViewController gestartet wird.
class MainViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in
if user != nil {
let LoginVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "usersVC")
self.present(LoginVc, animated: true, completion: nil)
}else {
let UserVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LoginVc")
self.present(UserVc, animated: true, completion: nil)
}
}
}
}
The MainViewController is still set as the rootviewcontroller in the AppDelegate.
I hope I helps someone else in the future!

Related

Swift - pushing ViewController not working after presenting ViewController again

The question sounds a bit confusing but I don't know how to describe it better.. Let me explain:
The first ViewController that gets presented is FirstLaunchVC, where he user types in his email and if he is registered he get's to LoginVC and from there he get's to MainVC. Everything is working fine.
In MainVC the user can sign out and get's back to FirstLaunchVC. However, after doing the weiterButton which should bring the user to LoginVC is not doing anything.
FirstLaunchVC:
#objc func weiterButtonTapped() {
email = emailTextfield.text!.trimmingCharacters(in: .whitespacesAndNewlines)
//prüfen, ob Email schon registriert ist
Auth.auth().fetchSignInMethods(forEmail: email) { (methods, error) in
//Email ist noch nicht registriert -> sign up
if methods == nil {
let SignUpView = self.storyboard?.instantiateViewController(withIdentifier: "SignUpVC") as! SignUpViewController
SignUpView.email = self.email
self.navigationController?.pushViewController(SignUpView, animated: false)
}
//Email ist registriert -> login
else {
print("hi")
self.view.endEditing(true)
let LoginView = self.storyboard?.instantiateViewController(withIdentifier: "LoginVC") as! LoginViewController
LoginView.email = self.email
self.navigationController?.pushViewController(LoginView, animated: true)
}
}
}
Main Problem:
The print(hi) is printing but pushViewController is not working after signing out.
LoginVC:
func transitionToHome () {
let homeVC =
storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.homeViewController) as? MainViewController
let navigationController = UINavigationController(rootViewController: homeVC!)
view.window?.rootViewController = navigationController
view.window?.makeKeyAndVisible()
}
MainVC:
This is where the user can sign out.
#objc func signoutButtonTapped() {
UserDefaults.standard.setIsLoggedIn(value: false)
UserDefaults.standard.synchronize()
let firstLaunchVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FirstLaunchVC")
self.navigationController?.present(firstLaunchVC, animated: true)
}
I tried to explain the problem the best I can. If anything is still unclear just let me know. I am happy for any help :)
if after sign out weiterButtonTapped() called,
you should put your pushController line in dispatch that's because controller be fully deallocated:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
self.navigationController?.pushViewController(LoginView, animated: true)
}

Swift 4 – Custom Alerts with protocols

I'm having issues with Custom alerts and sending actions back to VC from which alert was called.
I have two classes:
Factory
ConfirmationAllert
User journey I'm trying to achieve:
The user performs actions in the Factory class after he finishes I call ConfirmationAllert using such code:
func showAlert() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ConfirmationAllert")
myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
(view as? UIViewController)?.present(myAlert, animated: true, completion: nil)
}
In ConfirmationAllert class I have button, which:
dismisses alert
sends action to Factory - this action is to dismiss Factory VC and go back to previous VC.
First action completes successfully, the second action not working. I'm using protocols to send the second action to Factory VC, but something is not working, and I don't know what.
Here is my code:
Factory
final class FactoryViewController: UIViewController {
let alert = ConfirmationAllert()
#IBAction func didPressSave(_ sender: UIButton) {
showAlert()
}
func goToPreviousVc() {
alert.delegate = self
print("Inside factory") -> print don't get called
// navigationController?.popViewController(animated: true) -> none of this works
// dismiss(animated: true, completion: nil) -> none of this works
}
}
extension FactoryViewController: ConfirmationAllertDelegate {
func dismissVC() {
goToPreviousVc()
print("Go to previous")
}
}
ConfirmationAllert
protocol ConfirmationAllertDelegate {
func dismissVC()
}
class ConfirmationAllert: UIViewController {
var delegate: ConfirmationAllertDelegate?
#IBAction func didPressOk(_ sender: UIButton) {
self.delegate?.dismissVC()
}
}
I didn't include viewDidLoad methods as I'm not calling anything there.
My issue is that method goToPreviousVc() doesn't perform any actions.
Thank you in advance for your help!
I guess your problem is that you setup your ConfirmationAllertDelegate at goToPreviousVc that supposed to be called using that delegate.
Instead, try to set up you delegate when you creating myAlert object
let myAlert = storyboard.instantiateViewController(withIdentifier: "ConfirmationAllert")
(myAlert as? ConfirmationAllert).delegate = self
// the rest of your code
After that, your alert will have a delegate since it was created and when you press the button, it should work as you expect.
Try to use below code
func showAlert() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "ConfirmationAllert") as! ConfirmationAllert
myAlert.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
myAlert.modalTransitionStyle = UIModalTransitionStyle.crossDissolve
myAlert.delegate = self
present(myAlert, animated: true, completion: nil)
}

Get current ViewController in external method in Swift

I'm making a external function to check if user is already logged into Firebase.
My code is working, but in trying to ensure that current VC is dismissing in the end, I get an error.
My question is: How can i get the current VC or how can i use self. to reference the current VC that function is called?
class Helper{
static func checkIfLogged() {
Auth.auth().addStateDidChangeListener { auth, user in
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "HomeViewController")
UIApplication.shared.keyWindow?.rootViewController = controller
self.dismiss(animated: true, completion: nil) **** ERROR IS HERE ***
}
}
}
extension UIApplication{
class func getPresentedViewController() -> UIViewController? {
var presentViewController = UIApplication.shared.keyWindow?.rootViewController
while let pVC = presentViewController?.presentedViewController
{
presentViewController = pVC
}
return presentViewController
}
}
just add this extension and call : UIApplication. getPresentedViewController()

Change the screen in swift

Following is the storyboard of my app:
The app establishes a connection with the server on screen1 and all the communication with the server is performed in the code of this screen only. We make a request on screen4 and send it to server through the code of screen1 and app receives the response from the sever. If app gets successful response then app should show screen5, where I get error.
I wrote different lines of code but failed. Following are line of code which I am using now:
import UIKit
class logoViewController: UIViewController {
#IBOutlet weak var act: UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
self.act.startAnimating()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// syncreq function is called from the connectionViewController.
// connectionViewController is the common class for connecting to the remote server
func syncreq (JSONdata: AnyObject) { // Proceesing for PRMS response
// Getting the value from the JSON
var Successful = self.getIntFromJSON(JSONdata as NSDictionary, key: "Successful")
println("Value of Successful : \(Successful)")
if (Successful == 0){
//Method1 not worked
// let adduser = regVC()
// self.presentViewController(adducer, animated: true, completion: nil)
//Method2 not worked
//let adducer = self.storyboard?.instantiateViewControllerWithIdentifier("registrationID") as regVC
//self.navigationController?.pushViewController(adducer, animated: true)
//Method3 not worked
//let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("registrationID") as regVC
//self.navigationController?.pushViewController(secondViewController, animated: true)
//performSegueWithIdentifier("registrationID", sender: self)
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("registrationID") as RegisterViewController
self.presentViewController(setViewController, animated: false, completion: nil)
}
else if (Successful == 1){
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("mnuID") as menuViewController
self.presentViewController(setViewController, animated: false, completion: nil)
}
}
func getIntFromJSON(data: NSDictionary, key: String) -> Int {
let info : AnyObject? = data[key]
// println("Value of data[key] : \(key)")
if let info = data[key] as? Int {
println("Value of value for \(key) : \(info)")
return info
}
else {
return 0
}
}
}
I got the following error:
Warning: Attempt to present <project.screen5: 0x7a094790> on <project.ViewController: 0x79639d90> whose view is not in the window hierarchy!
screenshot of error:
Your problem is that view of the ViewController 1 is not in the window hierarchy, thus ViewController 1 cannot present modal VC.
Cleanest fix would be to change your app architecture design - having one view controller perform all the network requests may cause more complications than just this one.
However, for 'just make it work' solution you can present modal VC from navigation controller, i.e.
self.navigationController?.presentViewController(setViewController, animated: false, completion: nil)

Warning: Attempt to present * on * whose view is not in the window hierarchy - swift

I'm trying to present a ViewController if there is any saved data in the data model. But I get the following error:
Warning: Attempt to present * on *whose view is not in the window hierarchy"
Relevant code:
override func viewDidLoad() {
super.viewDidLoad()
loginButton.backgroundColor = UIColor.orangeColor()
var request = NSFetchRequest(entityName: "UserData")
request.returnsObjectsAsFaults = false
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
var results:NSArray = context.executeFetchRequest(request, error: nil)!
if(results.count <= 0){
print("Inga resultat")
} else {
print("SWITCH VIEW PLOX")
let internVC = self.storyboard?.instantiateViewControllerWithIdentifier("internVC") as internViewController
self.presentViewController(internVC, animated: true, completion: nil)
}
}
I've tried different solutions found using Google without success.
At this point in your code the view controller's view has only been created but not added to any view hierarchy. If you want to present from that view controller as soon as possible you should do it in viewDidAppear to be safest.
In objective c:
This solved my problem when presenting viewcontroller on top of mpmovieplayer
- (UIViewController*) topMostController
{
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
Swift 3
I had this keep coming up as a newbie and found that present loads modal views that can be dismissed but switching to root controller is best if you don't need to show a modal.
I was using this
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard?.instantiateViewController(withIdentifier: "MainAppStoryboard") as! TabbarController
present(vc, animated: false, completion: nil)
Using this instead with my tabController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let view = storyboard.instantiateViewController(withIdentifier: "MainAppStoryboard") as UIViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
//show window
appDelegate.window?.rootViewController = view
Just adjust to a view controller if you need to switch between multiple storyboard screens.
Swift 3.
Call this function to get the topmost view controller, then have that view controller present.
func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while (topController.presentedViewController != nil) {
topController = topController.presentedViewController!
}
return topController
}
Usage:
let topVC = topMostController()
let vcToPresent = self.storyboard!.instantiateViewController(withIdentifier: "YourVCStoryboardID") as! YourViewController
topVC.present(vcToPresent, animated: true, completion: nil)
for SWIFT
func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
while (topController.presentedViewController != nil) {
topController = topController.presentedViewController!
}
return topController
}
You just need to perform a selector with a delay - (0 seconds works).
override func viewDidLoad() {
super.viewDidLoad()
perform(#selector(presentExampleController), with: nil, afterDelay: 0)
}
#objc private func presentExampleController() {
let exampleStoryboard = UIStoryboard(named: "example", bundle: nil)
let exampleVC = storyboard.instantiateViewController(withIdentifier: "ExampleVC") as! ExampleVC
present(exampleVC, animated: true)
}
Swift 4
func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
while (topController.presentedViewController != nil) {
topController = topController.presentedViewController!
}
return topController
}
Use of main thread to present and dismiss view controller worked for me.
DispatchQueue.main.async { self.present(viewController, animated: true, completion: nil) }
I was getting this error while was presenting controller after the user opens the deeplink.
I know this isn't the best solution, but if you are in short time frame here is a quick fix - just wrap your code in asyncAfter:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.7, execute: { [weak self] in
navigationController.present(signInCoordinator.baseController, animated: animated, completion: completion)
})
It will give time for your presenting controller to call viewDidAppear.
For swift 3.0 and above
public static func getTopViewController() -> UIViewController?{
if var topController = UIApplication.shared.keyWindow?.rootViewController
{
while (topController.presentedViewController != nil)
{
topController = topController.presentedViewController!
}
return topController
}
return nil}
let storyboard = UIStoryboard(name: "test", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "teststoryboard") as UIViewController
UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil)
This seemed to work to make sure it's the top most view.
I was getting an error
Warning: Attempt to present myapp.testController: 0x7fdd01703990 on myapp.testController: 0x7fdd01703690 whose view is not in the window hierarchy!
Hope this helps others with swift 3
I have tried so many approches! the only useful thing is:
if var topController = UIApplication.shared.keyWindow?.rootViewController
{
while (topController.presentedViewController != nil)
{
topController = topController.presentedViewController!
}
}
All implementation for topViewController here are not fully supporting cases when you have UINavigationController or UITabBarController, for those two you need a bit different handling:
For UITabBarController and UINavigationController you need a different implementation.
Here is code I'm using to get topMostViewController:
protocol TopUIViewController {
func topUIViewController() -> UIViewController?
}
extension UIWindow : TopUIViewController {
func topUIViewController() -> UIViewController? {
if let rootViewController = self.rootViewController {
return self.recursiveTopUIViewController(from: rootViewController)
}
return nil
}
private func recursiveTopUIViewController(from: UIViewController?) -> UIViewController? {
if let topVC = from?.topUIViewController() { return recursiveTopUIViewController(from: topVC) ?? from }
return from
}
}
extension UIViewController : TopUIViewController {
#objc open func topUIViewController() -> UIViewController? {
return self.presentedViewController
}
}
extension UINavigationController {
override open func topUIViewController() -> UIViewController? {
return self.visibleViewController
}
}
extension UITabBarController {
override open func topUIViewController() -> UIViewController? {
return self.selectedViewController ?? presentedViewController
}
}
The previous answers relate to the situation where the view controller that should present a view 1) has not been added yet to the view hierarchy, or 2) is not the top view controller.
Another possibility is that an alert should be presented while another alert is already presented, and not yet dismissed.
Swift Method, and supply a demo.
func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.sharedApplication().keyWindow!.rootViewController!
while (topController.presentedViewController != nil) {
topController = topController.presentedViewController!
}
return topController
}
func demo() {
let vc = ViewController()
let nav = UINavigationController.init(rootViewController: vc)
topMostController().present(nav, animated: true, completion: nil)
}
Swift 5.1:
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
let mainViewController = storyboard.instantiateViewController(withIdentifier: "ID")
let appDeleg = UIApplication.shared.delegate as! AppDelegate
let root = appDeleg.window?.rootViewController as! UINavigationController
root.pushViewController(mainViewController, animated: true)
Rather than finding top view controller, one can use
viewController.modalPresentationStyle = UIModalPresentationStyle.currentContext
Where viewController is the controller which you want to present
This is useful when there are different kinds of views in hierarchy like TabBar, NavBar, though others seems to be correct but more sort of hackish
The other presentation style can be found on apple doc

Resources