check if user is logged in ios swift - ios

i want check in appdelegate.swift file if user logged in showing HomeviewController like Instagram and if not Showing LoginViewController , iam using mysql to save users data and php bridge lang
code in appdalegate.swift
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
var rootViewController = self.window!.rootViewController
let isUserLoggedIn:Bool = NSUserDefaults.standardUserDefaults().boolForKey("isUserLoggedIn")
if(isUserLoggedIn) {
let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let protectedPage = mainStoryboard.instantiateViewControllerWithIdentifier("goToHome") as! HomeViewController
window!.rootViewController = protectedPage
window!.makeKeyAndVisible()
}
else{
let mainStoryboard = UIStoryboard(name: "Main" , bundle: nil)
let loginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("loginview") as! LoginViewController
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}
return true
}
loginviewcontoler.swift
let success:NSInteger = jsonData.valueForKey("success") as! NSInteger
//[jsonData[#"success"] integerValue];
NSLog("Success: %ld", success);
if(success == 1)
{
NSLog("Login SUCCESS");
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
prefs.setObject(username, forKey: "USERNAME")
prefs.setInteger(1, forKey: "ISLOGGEDIN")
prefs.synchronize()
self.performSegueWithIdentifier("goToHome", sender: self)
ProfileViewController.swift
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
let prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
let isLoggedIn:Int = prefs.integerForKey("ISLOGGEDIN") as Int
if (isLoggedIn != 1) {
self.performSegueWithIdentifier("goto_login", sender: self)
}
else
{
self.usernameLabel.text = prefs.valueForKey("USERNAME") as? String
}
}
sorry for long question

Don't use in this situation NSUserDefaults
https://github.com/matthewpalmer/Locksmith
Here is good example of using safe LogIn / LogOut
so, after implementation
if let dictionary = Locksmith.loadDataForUserAccount("accaunt")
{
//go to profile
}
else
{
//go to login
}

Follow step:-
Step 1 :- Create custom SplashViewController set rootViewController SplashViewController
Step 2 :- Write login service call on SplashViewController
Setp 3 :- When you get Login success then just change rootViewController
Setp 4 :- Write one method in AppDelegate For change rootViewController for redirect Homescreen.
Setp 5 :- Call AppDelegate method for go to Homescreen.

You have to do some works.
1. You have to make sure that the user is logged in. to do that when you press the button (login button) use UserDefaults
let userDefault = UserDefaults.standard
userDefault.set(true, forKey: "isLoggedIn")
userDefault.synchronize()
2.When user will launch the app again just check whether the user logged in or not in viewDidAppear method/ viewDidLoad(its your choice which method you will use. most of the time I use viewDidAppear)
let userDefault = UserDefaults.standard
let savedData = userDefault.bool(forKey: "isLoggedIn")
if(savedData){
performSegue(withIdentifier: "segueTest", sender:nil)//here u have decide the which view will show if the user is logged in how. here i used segue.
}else{
viewController = self// this is the main view. just make the object of the class and called it.
}
I hope you know how to prepare segue

Just a concise way of doing the same thing:
func checkIfUserIsLoggedIn() {
if Auth.auth().currentUser == nil {
DispatchQueue.main.async {
// present login controller
let loginVC = LoginVC()
let navController = UINavigationController(rootViewController: loginVC)
self.present(navController, animated: true, completion: nil)
}
return
}
}

Related

How to skip 2 ViewController correctly

Tell me, please, what am I doing wrong?
There are 3 View:
The user selects his gender
The user enters his weight
Main View Application
After the user has written this data, he gets to the main screen of the application. I want to make the user choose his gender and weight only at the very first launch of the application. To do this, I created a UserSettings model to store the entered values there:
final class UserSettings {
enum SettingsKeys: String {
case userSex
case userWeight
}
static var userSex: String! {
get {
return UserDefaults.standard.string(forKey: SettingsKeys.userSex.rawValue)
}
set {
let defaults = UserDefaults.standard
let key = SettingsKeys.userSex.rawValue
if let sex = newValue {
print("Пол \(sex) добавлен в \(key)")
defaults.set(sex, forKey: key)
} else {
defaults.removeObject(forKey: key)
}
}
}
static var userWeight: String! {
get {
return UserDefaults.standard.string(forKey: SettingsKeys.userWeight.rawValue)
}
set {
let defaults = UserDefaults.standard
let key = SettingsKeys.userWeight.rawValue
if let weight = newValue {
print("Вес \(weight) добавлен в \(key)")
defaults.set(weight, forKey: key)
} else {
defaults.removeObject(forKey: key)
}
}
}
}
In AppDelegate in function didFinishLaunchingWithOptions launchOptions wrote:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let defaults = UserDefaults.standard
let skipPageSex = defaults.bool(forKey: UserSettings.userSex)
let skipPageWeight = defaults.bool(forKey: UserSettings.userWeight)
if skipPageSex && skipPageWeight == true {
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let nextView: MainViewController = mainStoryboard.instantiateViewController(identifier: "MainViewController") as! MainViewController
window?.rootViewController = nextView
self.window?.makeKeyAndVisible()
} else {
let firstStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let firstView: WelcomeViewController = firstStoryboard.instantiateViewController(identifier: "welcomeVC") as! WelcomeViewController
window?.rootViewController = firstView
self.window?.makeKeyAndVisible()
}
return true
}
In the Sex select View in the button action, I wrote:
#IBAction func manSelected(_ sender: UIButton) {
sender.setTitle("Male", for: .normal)
UserSettings.userSex = sender.currentTitle
let defaults = UserDefaults.standard
defaults.setValue(true, forKey: UserSettings.userSex)
defaults.synchronize()
}
And the same for the button with the female choice, only sender.setTitle("Female", for: .normal)
I tried removing UIMainStoryboardFile and UIApplicationSceneManifest in Info.plist and in this case I get just a black screen without errors. In the Storyboard ID I use the same value as in AppDelegate. Please, help
Try something like this. Initial with a TempViewController. Depend on your data, it will segue perform show Gender Picker or go directly to Main. Remember to check the Is Initial View Controller for root Navigation Controller.

Swift - app crashes after setting UserDefaults

I am trying to implement a "always logged in" function in to my app. The problem is that if I restart my app, it crashes. This is what I did:
set Userdefault:
#objc func loginButtonTapped(_ sender: Any) {
let email = self.emailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let password = self.passwordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
// start button animation
loginButton.startAnimation()
let qualityOfServiceClass = DispatchQoS.QoSClass.background
let backgorundQueue = DispatchQueue.global(qos: qualityOfServiceClass)
backgorundQueue.async {
// check if account details correct
Auth.auth().signIn(withEmail: email, password: password) { (result, error) in
if error != nil {
DispatchQueue.main.async {
// error -> stop animation
self.loginButton.stopAnimation(animationStyle: .shake, revertAfterDelay: 0) {
self.errorLabel.text = error!.localizedDescription
self.errorLabel.alpha = 1
}
}
}else {
// correct acount details -> login
DispatchQueue.main.async {
UserDefaults.standard.set(true, forKey: "isLoggedIn")
UserDefaults.standard.synchronize()
// transition to home ViewController
self.transitionToHome()
}
}
}
}
}
checking UserDefault:
class MainNavigationControllerViewController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = MainViewController()
viewControllers = [homeController]
}
}
fileprivate func isLoggedIn() -> Bool {
return UserDefaults.standard.bool(forKey: "isLoggedIn")
}
}
The user logs in via Firebase and all the data is stored in Cloud Firestore.
Error
cell.customWishlistTapCallback = {
let heroID = "wishlistImageIDX\(indexPath)"
cell.theView.heroID = heroID
let addButtonHeroID = "addWishButtonID"
self.addButton.heroID = addButtonHeroID
// track selected index
self.currentWishListIDX = indexPath.item
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as? WishlistViewController
vc?.wishList = self.dataSourceArray[self.currentWishListIDX]
// pass drop down options
vc?.theDropDownOptions = self.dropDownButton.dropView.dropDownOptions
vc?.theDropDownImageOptions = self.dropDownButton.dropView.dropDownListImages
// pass current wishlist index
vc?.currentWishListIDX = self.currentWishListIDX
// pass the data array
vc?.dataSourceArray = self.dataSourceArray
// set Hero ID for transition
vc?.wishlistImage.heroID = heroID
vc?.addWishButton.heroID = addButtonHeroID
// allow MainVC to recieve updated datasource array
vc?.dismissWishDelegate = self
vc?.theTableView.tableView.reloadData()
self.present(vc!, animated: true, completion: nil)
}
Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
at line:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "WishlistVC") as! WishlistViewController
I guess it is not as easy as I thought. Does anyone know why the app crashes and how I can solve this? :)
You are creating your MainViewController instance using a simple initialiser (MainViewController()) rather than instantiating it from the storyboard. As a result, any #IBOutlet properties will be nil since it is the the storyboard process that allocates those object instances and assigns them to the properties.
You need to add an identifier to your main view controller scene (if it doesn't already have one) and use that to instantiate the view controller instance. E.g. assuming the scene identifier is "MainScene" you would have something like:
override func viewDidLoad() {
super.viewDidLoad()
if isLoggedIn() {
let homeController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("MainScene")
viewControllers = [homeController]
}
}
The crash in your updated question indicates that either the scene with the identifier WishlistVC doesn't have its class set to WishlistViewController or it isn't found so the forced downcast crashes.

NSUserDefault Value found nil with kill the App

I just trying to save the user Id and status as true value in UserDefaults when they login or Register with App.
UserDefaults.standard.set(user?.CustID, forKey: "CustID")
UserDefaults.standard.set(true, forKey: "status")
so when the user reopen the app in my Appdelegate I check the Status if it's true then user directly pass on HomeScreen and if the status False its pass on login screen.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let status = UserDefaults.standard.bool(forKey: "status")
if (status == true) && (custID != "") {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "HomeTicketViewController") as! HomeTicketViewController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
}else {
let storyBoard : UIStoryboard = UIStoryboard(name: "Login", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
let navigationController = UINavigationController(rootViewController: nextViewController)
let appdelegate = UIApplication.shared.delegate as! AppDelegate
appdelegate.window!.rootViewController = navigationController
}
}
I get the status true but i found that The CustID found nil.
Is there any solution for that?
In userdefault if value not found in userdefaults corresponding to key then userdefault return nil and in case of bool it returns false.
In your case, when you app launches, app does not have any value corresponding to custID so you getting nil.
You need to add check like this
if (status == true) && (custID != nil){
:
:
}
The default is false
let status = UserDefaults.standard.bool(forKey: "status")
so status will be false at first , also status being true doesn't mean CustID saved no nil
UserDefaults.standard.set(user!.CustID, forKey: "CustID")
as user may be nil so force-unwrap to verify that

Back button disappears after creating AppDelegate in Swift 3.0

I created the structure as below in xcode 8 swift 3.0.
Before I add AppDelegate code. Back button still appear fine on Apply, Apply Behalf and Profile controller.
I use segue to open page.
But after I add AppDelegate code into Homepage and Login controllers , back button disappears on Apply, Apply behalf and profile controller page.
Can someone help or explain why is this happening ? How to enable back the back button on apply, apply behalf and profile page ?
Thanks.
Home.swift
import UIKit
class ViewController: UIViewController {
#IBOutlet var staffNumberLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
staffNumberLbl.text = UserDefaults.standard.object(forKey: "login") as? String
}
override func viewDidAppear(_ animated: Bool) {
let isUserLoggedIn = UserDefaults.standard.bool(forKey: "loggedIn")
if(!isUserLoggedIn){
self.performSegue(withIdentifier: "loginview", sender: self)
}
}
#IBAction func logoutData(_ sender: Any) {
UserDefaults.standard.set(false, forKey: "loggedIn")
UserDefaults.standard.synchronize();
let loginViewController = self.storyboard!.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
}
}
Login.swift
import UIKit
class LoginViewController: UIViewController {
#IBOutlet var loginlbl: UITextField!
#IBOutlet var passlbl: UITextField!
#IBOutlet var login_button: UIButton!
var login: String!
var pw: String!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func loginData(_ sender: Any) {
login = loginLbl.text
pw = passLbl.text
if(login == "" || pw == ""){
return
}
else{
let url = URL(string: "http://localhost/login.php")
let session = URLSession.shared
let request = NSMutableURLRequest(url: url! as URL)
request.httpMethod = "POST"
let LoginDataToPost = "login=\(login!)&pw=\(pw!)"
request.httpBody = LoginDataToPost.data(using: String.Encoding.utf8)
let task = session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) in
if error != nil {
return
}
else {
do {
if let json = try JSONSerialization.jsonObject(with: data!) as? [String: String]
{
DispatchQueue.main.async
{
let message = Int(json["message"]!)
let login = json["login"]
if(message == 1) {
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
UserDefaults.standard.set(login, forKey: "login")
UserDefaults.standard.synchronize();
self.dismiss(animated: true, completion: nil)
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = myViewController
appDelegate.window?.makeKeyAndVisible()
print("Value for login is : \(login!)")
return
}
else {}
}
}
else {}
}
catch let jsonParse {}
}
})
task.resume()
}
}
}
AppDelegate.swift
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let isUserLoggedIn:Bool = UserDefaults.standard.bool(forKey: "isUserLoggedIn")
if(!isUserLoggedIn) {
let loginViewController = mainStoryBoard.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
window!.rootViewController = loginViewController
window!.makeKeyAndVisible()
}
else {
let homePage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
window!.rootViewController = homePage
window!.makeKeyAndVisible()
}
return true
}
}
You are setting rootviewcontroller without embedding navigation controller to it in logoutData & loginData function.
Use this code :
let navigationController = UINavigationController.init(rootViewController: myViewController)
appDelegate.window?.rootViewController = navigationController
Use this code in AppDelegate:
if(!isUserLoggedIn) {
let loginViewController = mainStoryBoard.instantiateViewController(withIdentifier: "loginview") as! LoginViewController
let navigationController = UINavigationController.init(rootViewController: loginViewController)
appDelegate.window?.rootViewController = navigationController
window!.makeKeyAndVisible()
}
else {
let homePage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
let navigationController = UINavigationController.init(rootViewController: homePage)
appDelegate.window?.rootViewController = navigationController
window!.makeKeyAndVisible()
}
Remove this from Home.swift,
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = loginViewController
appDelegate.window?.makeKeyAndVisible()
because its not inheriting the properties of Navigation controller
and add it in Appdelegate.swift file
For the other 3 viewcontrollers, you need to add the Navigation controller between eachSegway in order to inherit it or code the button by instantiating the viewcontrollers respectively
After successful login,try to make NavigationController as rootViewController instead of your ViewController
Your back button will start appearing.
In AppDelegate, in else block, instead of this line
let homePage = mainStoryBoard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
write this
let homePage = mainStoryBoard.instantiateViewController(withIdentifier: "NavigationController") as! UINavigationController
Inside LoginViewController, in the block if(message == 1)
replace
let myViewController:ViewController = self.storyboard!.instantiateViewController(withIdentifier: "ViewController") as! ViewController
with
let navController:UINavigationController = self.storyboard!.instantiateViewController(withIdentifier: "NavigationController") as! UINavigationController
Also set storyboard identifier for UINavigationController in storyboard to NavigationController
Depending on your configuration:
self.navigationItem.hidesBackButton = YES;
OR:
self.navigationController.navigationItem.hidesBackButton = YES;
Or, if you just want to disable the button without hiding it, you can use this.
self.navigationController.navigationItem.backBarButtonItem.enabled = NO;

Why is this variable not printing in this function?

When ViewController loads, the correct value gets printed. But when tried to print the same value with a UIButton, the print is nil
ViewController
var userEmail: String?
func userLoggedIn(data: String) {
userEmail = data //sent from delagate
print(userEmail) // successfully printed
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loginView" {
let loginViewController: LoginViewController = segue.destination as! LoginViewController
loginViewController.delegate = self
}
}
override func viewDidAppear(_ animated: Bool) {
let isUserLoggedIn = UserDefaults.bool(UserDefaults.standard)(forKey: "isUserLoggedIn")
if(!isUserLoggedIn) {
self.performSegue(withIdentifier: "loginView", sender: self);
}
}
#IBAction func createCommunityTapped(_ sender: AnyObject) {
let createCommunityController = self.storyboard?.instantiateViewController(withIdentifier: "CreateNewCommunity") as! CreateNewCommunity
print ("now here ", userEmail) // prints nil
createCommunityController.myEmail = userEmail
}
The userEmail value is being passed from LoginViewController via this section of code:
if(returnValue != "error") {
self.delegate?.userLoggedIn(data: userEmail! )
UserDefaults.set(UserDefaults.standard)(true, forKey: "isUserLoggedIn");
let mainPage = self.storyboard?.instantiateViewController(withIdentifier: "ViewController")
let mainPageNav = UINavigationController(rootViewController: mainPage!)
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = mainPageNav
self.dismiss(animated: true, completion: nil)
}
AppDelegate:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let isUserLoggedIn = UserDefaults.bool(UserDefaults.standard)(forKey: "isUserLoggedIn")
if(!isUserLoggedIn) {
let loginViewController = mainStoryboard.instantiateViewController(withIdentifier: "LoginViewController")
self.window?.rootViewController = loginViewController
window!.makeKeyAndVisible()
} else {
let protectedPage = mainStoryboard.instantiateViewController(withIdentifier: "ViewController")
self.window?.rootViewController = protectedPage
window!.makeKeyAndVisible()
}
return true
}
The issue seems to lie in your Login view controller. You're passing the userEmail to your first instance of ViewController, then creating a new instance of ViewController and setting it as the root, which releases the first one. The first step should be to remove this code:
let mainPage = self.storyboard?.instantiateViewController(withIdentifier: "ViewController")
let mainPageNav = UINavigationController(rootViewController: mainPage!)
let appDelegate = UIApplication.shared.delegate
appDelegate?.window??.rootViewController = mainPageNav
In your AppDelegate, you should set the root view controller to ViewController, and modally present the LoginViewController on top of it if the user is not logged in. Make sure to set the LoginViewController's delegate to ViewController. Then your login logic can be:
if(returnValue != "error") {
self.delegate?.userLoggedIn(data: userEmail!)
UserDefaults.standard.set(true, forKey: "isUserLoggedIn")
self.dismiss(animated: true, completion: nil)
}

Resources