Appdelegate sigabrt not appearing anything ! in main storyboard - swift

I am making a newsfeed and when I try to add a post it appears this alert[SIGABRT]and not go to the next viewController to write the post here it's a picture , any idea?
and those are some ss from the breakpoint and exception.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
let authListener = Auth.auth().addStateDidChangeListener { auth, user in
let storyboard = UIStoryboard(name: "Main", bundle: nil )
if user != nil {
UserService.observeUserProfile(user!.uid) { UserProfile in
UserService.currentUserProfile = UserProfile
}
//
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
} else {
UserService.currentUserProfile = nil
let controller = storyboard.instantiateViewController(withIdentifier: "MenuViewController") as! ViewController
self.window?.rootViewController = controller
self.window?.makeKeyAndVisible()
}
}
return true
}
}

Related

userId coming to appdelegate still not going to Mainviewcontroller why? in swift

NavigationController is isinitial viewcontroller in storyboard, NavigationController embedded to LoginViewcontroller
in project storyboard navigation willbe like below
NavigationController->LoginViewcontroller-> RegistrationViewcontroller -> MainViewcontroller
in successful registration with PhNUmber i am getting userId which i have stored in KeychainWrapper
in RegistrationViewcontroller: i am storing userId like below:
let userID: String=jsonObj?["userId"] as? String ?? ""
KeychainWrapper.standard.set(userID, forKey: "USERID")
which i am checking in appdelegate like below to go Mainviewcontroller:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var savedUserId: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
}
return true
}
}
here savedUserId coming then why i am not going to MainViewcontroller, all the time LoginViewcontroller appears
You need to initialize the window. Give this line above setting the rootViewController:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var savedUserId: String?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window = UIWindow()
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
}
return true
}
}
Update: If you're using SceneDelegate you should use this:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let windowScene = scene as? UIWindowScene else { return }
window = UIWindow(windowScene: windowScene)print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window = UIWindow()
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
}
}
}
Try using this extension
// add this function in your AppDelegate
func makeRootVC(_ storyBoardName : String, _ vcName : String) {
let vc = UIStoryboard(name: storyBoardName, bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let nav = UINavigationController(rootViewController: vc)
nav.navigationBar.isHidden = true
self.window?.rootViewController = nav
let options: UIView.AnimationOptions = .transitionCrossDissolve
let duration: TimeInterval = 0.6
UIView.transition(with: self.window!, duration: duration, options: options, animations: {}, completion: nil)
}
then use it like this in your didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
savedUserId = KeychainWrapper.standard.string(forKey: "USERID")
print("appdelegate userid \(savedUserId)")
if savedUserId != nil{
print("saveuserid \(savedUserId)")
self.makeRootVC("Main","MainViewController")
}
return true
}
check if window is nil and add makeKeyAndVisible() after setting rootViewController in AppDelegate
if(window == nil){
self.window = UIWindow(frame:UIScreen.main.bounds)
}
if savedUserId != nil{
print("saveuserid \(savedUserId)")
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
window?.rootViewController = mainStoryBoard.instantiateViewController(withIdentifier: "MainViewController")
self.window?.makeKeyAndVisible()
}

'Auto-login' not working anymore

I am using a firebase project for my app, on which users have to create an account to access to the content of the app.
In order to avoid users having to log in each time they launch the app, I put (in the AppDelegate) :
if Auth.auth().currentUser != nil {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "tabBar")
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
} else {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "InscriptionViewController")
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()
}
And this piece of code did work for the past months but now it is not (all my pods are up to date)
Why is it not working anymore ?
Your AppDelegate should looks like this.
import UIKit
import Firebase
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FirebaseApp.configure()
if Auth.auth().currentUser != nil {
//your code
}
}
}
Try to download google-info.plist

Swift, Firebase - Present different ViewControllers in AppDelegate if User is Logged-in

I'm using Firebase for my app and I want to present different view controllers in AppDelegate with the code below but I keep getting an error.
import UIKit
import Firebase
import FirebaseAuth
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow()
if Auth.auth().currentUser != nil {
window?.rootViewController = HomeController()
} else {
window?.rootViewController = LoginController()
}
return true
}
...
This is the error.
libc++abi.dylib: terminating with uncaught exception of type
NSException
(lldb)
How can I fix this problem?
Swift 4.0
You can use this function as like below in AppDelegate.
func configureWindowAndMakeVisible(rootVC: UIViewController) {
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
window.rootViewController = rootVC
window.makeKeyAndVisible()
}
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if Auth.auth().currentUser != nil {
// Use initialization (i.e HomeController()) only if you had initialize view in Controller
configureWindowAndMakeVisible(rootVC: HomeController())
} else {
configureWindowAndMakeVisible(rootVC: LoginController())
}
return true
}
First of all, you need to know that you need a reference to the already created instance instead of creating a new one. There can be two case
Case 1. If you want to change the initial view controller on the check of if Auth.auth().currentUser == nil or iAuth.auth().currentUser != nil then you can simply write the code in didFinishLaunchingWithOptions in AppDelegate and make sure you must give the identifier in storyboard and use the same identifier in the code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
if Auth.auth().currentUser != nil {
let viewcontroller : MainViewController = mainView.instantiateViewController(withIdentifier: "MainViewIdentifier") as! MainViewController
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
} else {
let viewcontroller : LoginViewController = mainView.instantiateViewController(withIdentifier: "loginViewIdentifier") as! LoginViewController
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
}
return true
}
Case 2. Suppose you have Main/Home/Dash VC (View controller) and it’s always is the initial view controller then there you have implemented the check if Auth.auth().currentUser == nil then present/push the Login view controller else not.
You are Direct Creating New Instance of ViewController
Try the following code
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if Auth.auth().currentUser != nil {
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
let viewcontroller : HomeController = mainView.instantiateViewController(withIdentifier: "someViewController") as! HomeController
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
} else {
var mainView: UIStoryboard!
mainView = UIStoryboard(name: "MainStoryboard", bundle: nil)
let viewcontroller : LoginController = mainView.instantiateViewController(withIdentifier: "loginVCIdentifier") as! LoginController
self.window!.rootViewController = viewcontroller
self.window?.makeKeyAndVisible();
}
return true
}
Make Sure you have given identifier to controller from storyboard

Changed from Navigation to TabBar, CoreData does not load

I changed my Navigation Controller to Tab Bar Controller, then I wanted to compile the app. But it does not load my managedContext anymore.
This is my launching code when I load the app:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
guard let navController = window?.rootViewController as? UINavigationController,
let viewController = navController.topViewController as? ViewController else {
return true
}
viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return true
}
Therefore I tried to edit the code like this:
Edit:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateInitialViewController()
//let controller = storyboard.instantiateViewController(withIdentifier: "TabBarVC")
self.window?.rootViewController = controller
guard let tabController = window?.rootViewController as? UITabBarController,
let navController = tabController.viewControllers![0] as? UINavigationController,
let viewController = navController.topViewController as? ViewController else {
return true
}
// Version that works for UINavigationCOntroller
// guard let navController = window?.rootViewController as? UINavigationController,
// let viewController = navController.topViewController as? ViewController else {
// return true
// }
viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return true
}
I changed the code, but I still can not load managedContext, and it returns nil when I want to use it in ViewController, I also set the initial Viewcontroller to the tabBarController.
Set rootviewcontroller for your window first and then right your logic afterwards.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let storyboard = UIStoryboard(<StoryboardName>, bundle: nil)
let controller = storyboard.initializeInitialViewController()
window.rootViewController = controller
guard let navController = window?.rootViewController as? UITabBarController,
let viewController = navController.moreNavigationController as? ViewController else {
return true
}
viewController.managedContext = coreDataStack.managedContext
// Override point for customization after application launch.
return

Setting Intialview controller embedded in UINavigationController

I have few view controllers embedded in UINavigationController. The first view controller is login page. The second view controller is the home page. I want initialview controller as second view controller when the user is already logged in.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
// Override point for customization after application launch.
if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
{
if let userAccessToken = data["accessToken"]
{
if (userAccessToken as! String) != ""
{
let initialViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
loginResponse = LoginResponse()
loginResponse?.UserAccessToken = userAccessToken as? String
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
}
return true
}
The problem is the subsequent view controllers are not embedded in navigation controller. Since it is not embedded in navigation controller I am not able to goback from one view controller to the other.
Add this in App delegate
first check user already login or not, if login then execute this code
let mainStoryBoard = UIStoryboard(name: "Main", bundle: nil)
let redViewController = mainStoryBoard.instantiateViewControllerWithIdentifier("respectiveIdentifier") as! ViewController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = redViewController
Use this in App Delegate and add an extension of UIStoryboard.
func checkForAlreadyLogin() {
let dashBoardScreen = UIStoryboard.dashBoardScreen()
let loginController = UIStoryboard.loginController()
if UserDefaults.standard.bool(forKey: UserDefaultValues().LOGINSTATUS){
self.window!.rootViewController = dashBoardScreen
}else {
self.window!.rootViewController = loginController
}
}
public extension UIStoryboard {
class func mainStoryboard() -> UIStoryboard { return UIStoryboard(name: "Main", bundle: Bundle.main) }
class func dashBoardScreen() -> HomeViewController?{
return mainStoryboard().instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController
}
internal class func loginController() -> LoginViewController?{
return mainStoryboard().instantiateViewController(withIdentifier: "LoginViewController") as? LoginViewController
}
}
Just call this checkForAlreadyLogin() method in 'didFinishLaunchingWithOptions'.
Also remember to set StoryboardID in the storyboard for each viewController.
This code did the trick for me.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let data = Locksmith.loadDataForUserAccount(userAccount: "someString")
{
if let userAccessToken = data["accessToken"]
{
if (userAccessToken as! String) != ""
{
let initialViewController = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "HomePageViewController") as! HomePageViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(initialViewController, animated: true)
}
}
}
return true
}